user19704437
user19704437

Reputation: 43

Unable to retrieve data by column name in Python

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]


Upvotes: 0

Views: 35

Answers (1)

kaann.gunerr
kaann.gunerr

Reputation: 170

You should use this dictionary=True

mycursor = mydb.cursor(buffered=True, dictionary=True)

Upvotes: 1

Related Questions