I was looking for a way to better represent the output we get after running a query on a MySQL table via pymysql and stumbled upon DictCursor
.
I gave it a shot and was able to achieve the following
import pymysql
# Connect to mysql database
conn = pymysql.connect('db_conn_str', 'user_r',
'pwd_r', 'db_name')
# Run a query to list all tables in database
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute('show tables')
tables = cur.fetchall()
# We get a list of dictionaries
print(tables)
I get an output as follows
[
{'Tables_in_db_name': 'table_x'},
{'Tables_in_db_name': 'table_y'},
{'Tables_in_db_name': 'table_z'}
]
I was wondering if the key Tables_in_db_name
is something we can define ourselves? If not how are the name of the keys picked.