python 数据库查询返回list或tuple实例


Posted in Python onMay 15, 2020

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实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
Dec 04 Python
Python 文件操作的详解及实例
Sep 18 Python
Python入门学习指南分享
Apr 11 Python
Python中pandas模块DataFrame创建方法示例
Jun 20 Python
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
Jun 26 Python
python requests抓取one推送文字和图片代码实例
Nov 04 Python
python实现双色球随机选号
Jan 01 Python
Python如何发送与接收大型数组
Aug 07 Python
解决python和pycharm安装gmpy2 出现ERROR的问题
Aug 28 Python
Python系统公网私网流量监控实现流程
Nov 23 Python
浅谈Selenium 控制浏览器的常用方法
Dec 04 Python
解决pytorch 损失函数中输入输出不匹配的问题
Jun 05 Python
Python基于gevent实现高并发代码实例
May 15 #Python
Django bulk_create()、update()与数据库事务的效率对比分析
May 15 #Python
Python实现aes加密解密多种方法解析
May 15 #Python
django 数据库 get_or_create函数返回值是tuple的问题
May 15 #Python
重写django的model下的objects模型管理器方式
May 15 #Python
Python基于pip实现离线打包过程详解
May 15 #Python
Django在Model保存前记录日志实例
May 14 #Python
You might like
memcached 和 mysql 主从环境下php开发代码详解
2010/05/16 PHP
php addslashes及其他清除空格的方法是不安全的
2012/01/25 PHP
php导入导出excel实例
2013/10/25 PHP
PHP微信公众号自动发送红包API
2016/06/01 PHP
php版微信公众平台入门教程之开发者认证的方法
2016/09/26 PHP
php监测数据是否成功插入到Mysql数据库的方法
2016/11/25 PHP
php下的原生ajax请求用法实例分析
2020/02/28 PHP
实例:尽可能写友好的Javascript代码
2006/10/09 Javascript
javascipt:filter过滤介绍及使用
2014/09/10 Javascript
JavaScript截取指定长度字符串点击可以展开全部代码
2015/12/04 Javascript
jQuery实现简单的点赞效果
2020/05/29 Javascript
js/jq仿window文件夹框选操作插件
2017/03/08 Javascript
详解Node项目部署到云服务器上
2017/07/12 Javascript
基于jQuery的表单填充实例
2017/08/22 jQuery
Vue2.0父组件与子组件之间的事件发射与接收实例代码
2017/09/19 Javascript
Three.js实现浏览器变动时进行自适应的方法
2017/09/26 Javascript
小程序如何使用分包加载的实现方法
2019/05/22 Javascript
js实现踩五彩块游戏
2020/02/08 Javascript
JavaScript ES6 Class类实现原理详解
2020/05/08 Javascript
Vue 电商后台管理项目阶段性总结(推荐)
2020/08/22 Javascript
解决Python传递中文参数的问题
2015/08/04 Python
python selenium UI自动化解决验证码的4种方法
2018/01/05 Python
django如何连接已存在数据的数据库
2018/08/14 Python
Python3使用Matplotlib 绘制精美的数学函数图形
2019/04/11 Python
如何使用Python实现斐波那契数列
2019/07/02 Python
python中什么是面向对象
2020/06/11 Python
python中元组的用法整理
2020/06/15 Python
纯CSS3实现3D旋转书本效果
2016/03/21 HTML / CSS
Kneipp克奈圃美国官网:德国百年精油配方的传承
2018/02/07 全球购物
门诊挂号室室长岗位职责
2013/11/27 职场文书
单位在职证明范本
2014/01/09 职场文书
魅力教师事迹材料
2014/01/10 职场文书
校园公益广告语
2014/03/13 职场文书
公司人事专员岗位职责
2014/08/11 职场文书
2015年元旦联欢晚会活动总结
2014/11/28 职场文书
【DOTA2】总决赛血虐~ XTREME GAMING vs MAGMA - OGA DOTA PIT 2022 CN
2022/04/02 DOTA