欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年05月15日 09:38:49   作者:chdeWang  
這篇文章主要介紹了python 數(shù)據(jù)庫(kù)查詢返回list或tuple實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

默認(rèn)程序:

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)

我的實(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python的random和time模塊詳解

    python的random和time模塊詳解

    這篇文章主要介紹了python的random和time模塊,具有一定借鑒價(jià)值,需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-10-10
  • python中assert用法實(shí)例分析

    python中assert用法實(shí)例分析

    這篇文章主要介紹了python中assert用法,實(shí)例分析了assert的功能及相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Python3標(biāo)準(zhǔn)庫(kù)之threading進(jìn)程中管理并發(fā)操作方法

    Python3標(biāo)準(zhǔn)庫(kù)之threading進(jìn)程中管理并發(fā)操作方法

    這篇文章主要介紹了Python3標(biāo)準(zhǔn)庫(kù)之threading進(jìn)程中管理并發(fā)操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python高級(jí)特性和高階函數(shù)及使用詳解

    python高級(jí)特性和高階函數(shù)及使用詳解

    Python很棒,它有很多高級(jí)用法值得細(xì)細(xì)思索,學(xué)習(xí)使用。這篇文章主要介紹了python高級(jí)特性和高階函數(shù)及使用詳解,需要的朋友可以參考下
    2018-10-10
  • Python處理excel與txt文件詳解

    Python處理excel與txt文件詳解

    大家好,本篇文章主要講的是Python處理excel與txt文件詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python實(shí)現(xiàn)全盤掃描搜索功能的方法

    python實(shí)現(xiàn)全盤掃描搜索功能的方法

    今天小編就為大家分享一篇python實(shí)現(xiàn)全盤掃描搜索功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 舉例講解Python設(shè)計(jì)模式編程的代理模式與抽象工廠模式

    舉例講解Python設(shè)計(jì)模式編程的代理模式與抽象工廠模式

    這篇文章主要介紹了Python編程的代理模式與抽象工廠模式,文中舉了兩個(gè)簡(jiǎn)單的小例子來(lái)說(shuō)明這兩種設(shè)計(jì)模式的思路在Python編程中的體現(xiàn),需要的朋友可以參考下
    2016-01-01
  • Python Websocket服務(wù)端通信的使用示例

    Python Websocket服務(wù)端通信的使用示例

    這篇文章主要介紹了Python Websocket服務(wù)端通信的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • python的一些加密方法及python 加密模塊

    python的一些加密方法及python 加密模塊

    這篇文章主要介紹了python的一些加密方法及python加密模塊,本文通過(guò)實(shí)例文字相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • 基于flask實(shí)現(xiàn)五子棋小游戲

    基于flask實(shí)現(xiàn)五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了基于flask實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評(píng)論