Python查询MysSQL时候输出字典类型
程序员文章站
2022-04-01 08:47:59
...
代码
import pymysql
connect = pymysql.connect(user="root", host="localhost", database="你的数据库", password="你的密码")
cursor = connect.cursor()
sql = "select * from table limit 10"
cursor.execute(sql)
infos = cursor.description
fileds = [info[0] for info in infos]
datas = cursor.fetchall()
for data in datas:
print(dict(zip(fileds, data)))