Python Direct

for various datbases:

pip install pymysql

And we can connect to a database:

conn = pymysql.connect(host='localhost',
                       user='root',
                       password='xxxxxx',
                       db='test',
                       charset='utf8mb4',
                       cursorclass=pymysql.cursors.DictCursor)

Queries - Select, Insert etc

sql = "select * from test.mule_test"
try:
    with conn.cursor() as cursor:
        cursor.execute(sql)
        result = cursor.fetchone()
        print(result)
finally:
    conn.close()

Pandas Approach

import pandas as pd
 
sql = "select * from test.mule_test"
try:
    df = pd.read_sql_query(sql, conn)
    print (df.tail(10) )
finally:
    conn.close()

with a result like:

   id                           data
0   1  {"pid": 101, "name": "name1"}
 
python_database.txt · Last modified: 2018/12/12 04:06 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd