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

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

1 import pymysql
2 def export(table_name):
3     conn =pymysql.connect(host = '118.24.3.40',
4                           user = 'jxz',password='123456',
5                           db='jxz',port=3306,charset = 'utf8')
6     cur = conn.cursor()
7     cur.execute('select * from %s'%table_name)
8     print(cur.fetchall())
9 export('app_student')

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

1 import pymysql
2 def export(table_name):
3     conn =pymysql.connect(host = '118.24.3.40',
4                           user = 'jxz',password='123456',
5                           db='jxz',port=3306,charset = 'utf8')
6     cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
7     cur.execute('select * from %s'%table_name)
8     print(cur.fetchall())
9 export('app_student')

 

posted on 2019-04-04 20:33  羽竹  阅读(6695)  评论(0编辑  收藏  举报