Python, MySQL and SELECT output to dictionary with column names for keys
Please use dictionary cursor:
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
For me, this worked:
cursor = conn.cursor(dictionary=True)
Detailed example:
import mysql.connector # pip install mysql-connector-python
conn = mysql.connector.connect(host="localhost", user="user", passwd="pass", database="dbname")
cursor = conn.cursor(dictionary=True)
sql = "SELECT * FROM `table` WHERE 1"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
row["col"]