您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了python如何實現數據庫查詢返回list或tuple,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
MySQLdb默認查詢結果都是返回tuple,輸出時候不是很方便,必須按照0,1這樣讀取,無意中在網上找到簡單的修改方法,就是傳遞一個cursors.DictCursor就行。
默認程序:
import MySQLdb db = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='test') cur = db.cursor() cur.execute('select * from user') rs = cur.fetchall() print rs # 返回類似如下 # ((1000L, 0L), (2000L, 0L), (3000L, 0L))
修改后:
import MySQLdb import MySQLdb.cursors db = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='test', cursorclass=MySQLdb.cursors.DictCursor) cur = db.cursor() cur.execute('select * from user') rs = cur.fetchall() print rs # 返回類似如下 # ({'age': 0L, 'num': 1000L}, {'age': 0L, 'num': 2000L}, {'age': 0L, 'num': 3000L})
或者也可以用下面替換connect和cursor部分
db = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='test')
cur = db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
我的實踐:
輸出為元組類型:
import pymysql db = pymysql.connect("localhost", "root", "123456", "filestore") cursor = db.cursor() sql='select * from tablelist where id>%s' %4 #查詢方法一 cursor.execute(sql) result=cursor.fetchall() print('result',result) sql2='select * from tablelist where id>%s' values=('4') # 此處為元組類型 #查詢方法二 cursor.execute(sql2,values) result2=cursor.fetchall() print('result2',result2) id_list=[] tablename_list=[] tabletime_lsit=[] cursor.execute('select * from tablelist where id>%s',[4,]) result3=cursor.fetchall() print('type(result3)',type(result3)) #對((6, 'engineeringdata20180901', '1535731200'),)類型數據的提取 for i in range(len(result3)): id_list.append(result3[i][0]) tablename_list.append(result3[i][1]) tabletime_lsit.append(result3[i][2]) print(id_list) print(tabletime_lsit) print(tablename_list) cursor.close() db.close() #輸出結果: result ((6, 'engineeringdata20180901', '1535731200'), (618, 'engineeringdata20180904', '1535990400')) result2 ((6, 'engineeringdata20180901', '1535731200'), (618, 'engineeringdata20180904', '1535990400')) type(result3) <class 'tuple'> [6, 618] ['1535731200', '1535990400'] ['engineeringdata20180901', 'engineeringdata20180904']
輸出為list類型:
list_id=[] list_tablename=[] list_tabletime=[] list=get_list('select * from tablelist where id>%s',[4]) print('list:',list) # 對[{'id': 6, 'tablename': 'engineeringdata20180901', 'tabletime': '1535731200'},]類型數據的提取 for i in range(len(list)): print(list[i]) list_id.append(list[i]['id']) list_tablename.append(list[i]['tablename']) list_tabletime.append(list[i]['tabletime']) print('list_id:',list_id) print('list_tabletime:',list_tabletime) print('list_tablename:',list_tablename) # 輸出結果為: list: [{'id': 6, 'tablename': 'engineeringdata20180901', 'tabletime': '1535731200'}, {'id': 618, 'tablename': 'engineeringdata20180904', 'tabletime': '1535990400'}] {'id': 6, 'tablename': 'engineeringdata20180901', 'tabletime': '1535731200'} {'id': 618, 'tablename': 'engineeringdata20180904', 'tabletime': '1535990400'} list_id: [6, 618] list_tabletime: ['1535731200', '1535990400'] list_tablename: ['engineeringdata20180901', 'engineeringdata20180904']
補充知識:python下 將 pymysql 返回的元組數據轉換為列表
我就廢話不多說了,大家還是直接看代碼吧!
from itertools import chain ... sql="select elems from table" cursor.execute(sql) elems = cursor.fetchall() resultlist = list(chain.from_iterable(elems)) ...
以上就是關于python如何實現數據庫查詢返回list或tuple的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。