python操作mysql数据-执行语句返回值直接返回字典类型

fetchall()将结果放在二维数组里面,每一行的结果在元组里面

 import pymysql
def export(table_name):
conn =pymysql.connect(host = '118.24.3.40',
user = 'jxz',password='',
db='jxz',port=3306,charset = 'utf8')
cur = conn.cursor()
cur.execute('select * from %s'%table_name)
print(cur.fetchall())
export('app_student')

想返回字典格式,只需要在建立游标的时候加个参数,cursor=pymysql.cursors.DictCursor。这样每行返回的值放在字典里面,然后整体放在一个list里面。

 import pymysql
def export(table_name):
conn =pymysql.connect(host = '118.24.3.40',
user = 'jxz',password='',
db='jxz',port=3306,charset = 'utf8')
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
cur.execute('select * from %s'%table_name)
print(cur.fetchall())
export('app_student')
上一篇:jenkins bat删除指定路径下的文件及文件夹


下一篇:Android Studio 如何在TextView中设置图标并按需调整图标大小