I have to retrieve data from a database. Right now i am using the following code:
import mysql.connector
connection = mysql.connector.connect(user, password, host, database)
cursor = connection.cursor()
cursor.execute(*Query*)
data = cursor.fetchall()
name = data[0][0]
surname = data[0][1]
I would need to access the data by field name instead of raw indexes. For instance something like that
name = data[0]['name']
surname = data[0]['surname']
Thanks
cursor = cnx.cursor(dictionary=True)in python 3dictionaryis available since Connector/Python 2.0.0. Apparently not restricted to Python 3for row in cursor: print("* {mykey}".format(Name=row['mykey'])), i have a column named "mykey" but i get a Keyerror: 'mykey'