python操做mysql數據-執行語句返回值直接返回字典類型

fetchall()將結果放在二維數組裏面,每一行的結果在元組裏面mysql

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裏面。sql

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')
相關文章
相關標籤/搜索