亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 數(shù)據(jù)庫(kù)查詢返回list或tuple實(shí)例

瀏覽:80日期:2022-07-25 15:11:49

MySQLdb默認(rèn)查詢結(jié)果都是返回tuple,輸出時(shí)候不是很方便,必須按照0,1這樣讀取,無(wú)意中在網(wǎng)上找到簡(jiǎn)單的修改方法,就是傳遞一個(gè)cursors.DictCursor就行。

默認(rèn)程序:

import MySQLdbdb = 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 MySQLdbimport MySQLdb.cursorsdb = 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)

我的實(shí)踐:

輸出為元組類型:

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))#對(duì)((6, ’engineeringdata20180901’, ’1535731200’),)類型數(shù)據(jù)的提取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()#輸出結(jié)果: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)# 對(duì)[{’id’: 6, ’tablename’: ’engineeringdata20180901’, ’tabletime’: ’1535731200’},]類型數(shù)據(jù)的提取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)# 輸出結(jié)果為: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’]

補(bǔ)充知識(shí):python下 將 pymysql 返回的元組數(shù)據(jù)轉(zhuǎn)換為列表

我就廢話不多說(shuō)了,大家還是直接看代碼吧!

from itertools import chain...sql='select elems from table'cursor.execute(sql)elems = cursor.fetchall()resultlist = list(chain.from_iterable(elems))...

以上這篇python 數(shù)據(jù)庫(kù)查詢返回list或tuple實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 91久久亚洲精品国产一区二区 | 亚洲免费影院 | 亚洲欧美韩日 | 亚洲 成人 欧美 自拍 | 在线观看91精品国产入口 | 日韩免费精品一级毛片 | 欧美日韩黄色大片 | 深夜偷偷看视频在线观看 | 日韩视频一区二区在线观看 | 国产在线观看成人 | 韩国中文字幕在线观看 | 国产网友自拍 | 99视频在线观看高清 | 91香蕉视频破解版 | 亚洲欧洲精品久久 | 国产成人精品视频2021 | 全黄a免费一级毛片人人爱 全黄一级裸片视频在线观看 | 在线精品自拍亚洲第一区 | 久久国产视频在线观看 | 国产在线高清理伦片a | 国产一区二区三区手机在线观看 | 久久五十路 | 久久久久无码国产精品一区 | 国内精品91最新在线观看 | 久久精品首页 | 超h福利视频在线观看 | 成人性欧美丨区二区三区 | 毛片国产 | 国产综合成色在线视频 | 99久久国语对白精品露脸 | 亚洲高清视频在线观看 | 分享一个无毒不卡免费国产 | 亚洲青草视频 | 99综合色| 午夜剧场福利社 | 日韩一级免费视频 | 91香蕉视频苹果 | 欧美成人精品手机在线观看 | 国产精品第2页 | a级国产乱理论片在线观看看 | 欧美黑人巨大肥婆性视频 |