Unable to retrieve data by column name in Python

Question:

I want to get the data by column name but when I type the column name I get an error.I want to use it like this: user[‘username’], This is my python code:

mydb = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
                               user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor = mydb.cursor(buffered=True)

mycursor.execute("select * from users")
users = mycursor.fetchall()

for user in users:
   username = user[2]


Asked By: user19704437

||

Answers:

You should use this dictionary=True

mycursor = mydb.cursor(buffered=True, dictionary=True)
Answered By: kaann.gunerr
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.