pymysql查询,返回字典格式。

使用:pymysql.cursors.DictCursor

直接上代码

import pymysql

db = pymysql.connect(host = "host", user = "user", password = "password", database = "database")
cursor = db.cursor(pymysql.cursors.DictCursor)

sql = "SELECT `id`,`name` FROM `student`;"
cursor.execute(sql)
data_list = cursor.fetchall()
cursor.close()
db.close()

posted @ 2021-06-16 10:07  做个笔记  阅读(684)  评论(0编辑  收藏  举报