简体   繁体   中英

how can we fetch details of particular row from mysql database using variable in python

How can we fetch details of particular row from mysql database using variable in python?

I want to print the details of particular row using variable from my database and I think I should use something like this:

data = cur.execute("SELECT * FROM loginproject.Pro WHERE Username = '%s';"% rob)

But this is showing only the index value, not the data. Please help me out.

使用cur.execute(query)执行查询后,您需要调用fetchall函数以从游标获取数据,例如:

data = cur.fetchall()

Something like this?

cur = db.cursor()
cur.execute("SELECT * FROM loginproject.Pro WHERE Username = '%s';"% rob)
result = cur.fetchall()
for row in result:
  print(row[0])

https://github.com/prosaigon/Python-Connector-Mysql

python3.6 library: pip install mysqlclient

use cursor.fetchall and cursor.description is your case

cursor.fetchall():

This will provide you all the rows fetched in a tuple format.

cursor.description:

This will provide you the list of columns in the query

And the one will go like :

cur.execute("SELECT * FROM loginproject.Pro WHERE Username = '%s';"% rob)
descriptor = cur.description
result = cur.fetchall()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM