Python查询Mysql时返回字典结构的代码


Posted in Python onJune 18, 2012

MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。
默认程序:
MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。默认程序:

import MySQLdb 
db = MySQLdb.connect(host = ´localhost´, user = ´root´, passwd = ´123456´, db = ´test´) 
cursor = db.cursor() 
cursor.execute(´select * from table´) 
rs = cursor.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) 
cursor = db.cursor() 
cursor.execute(´select * from table´) 
rs = cursor.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´) 
cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
Python 相关文章推荐
python基础教程之获取本机ip数据包示例
Feb 10 Python
linux安装Python3.4.2的操作方法
Sep 28 Python
Python中垃圾回收和del语句详解
Nov 15 Python
Python设计模式之代理模式实例详解
Jan 19 Python
利用Python校准本地时间的方法教程
Oct 31 Python
用python中的matplotlib绘制方程图像代码
Nov 21 Python
python opencv 实现对图像边缘扩充
Jan 19 Python
详解python环境安装selenium和手动下载安装selenium的方法
Mar 17 Python
virtualenv介绍及简明教程
Jun 23 Python
Python 实现集合Set的示例
Dec 21 Python
python中subplot大小的设置步骤
Jun 28 Python
python 实现堆排序算法代码
Jun 05 #Python
python 实现归并排序算法
Jun 05 #Python
python 实现插入排序算法
Jun 05 #Python
python 算法 排序实现快速排序
Jun 05 #Python
python操作MySQL数据库的方法分享
May 29 #Python
python利用elaphe制作二维条形码实现代码
May 25 #Python
用python实现批量重命名文件的代码
May 25 #Python
You might like
四月新番又没了,《Re:从零开始的异世界生活》第二季延期至7月播出
2020/05/06 日漫
Google Voice 短信发送接口PHP开源版(2010.5更新)
2010/07/22 PHP
PHP动态生成javascript文件的2个例子
2014/04/11 PHP
PHP simplexml_import_dom()函数讲解
2019/02/03 PHP
从数据库读取数据后将其输出成html标签的三种方法
2014/10/13 Javascript
JavaScript子窗口调用父窗口变量和函数的方法
2015/10/09 Javascript
vue.js表格组件开发的实例详解
2016/10/12 Javascript
AngularJs导出数据到Excel的示例代码
2017/08/11 Javascript
详解node child_process模块学习笔记
2018/01/24 Javascript
微信小程序的部署方法步骤
2018/09/04 Javascript
详解React 服务端渲染方案完美的解决方案
2018/12/14 Javascript
Python  连接字符串(join %)
2008/09/06 Python
Python的设计模式编程入门指南
2015/04/02 Python
numpy自动生成数组详解
2017/12/15 Python
Python编程把二叉树打印成多行代码
2018/01/04 Python
Python使用try except处理程序异常的三种常用方法分析
2018/09/05 Python
Python multiprocess pool模块报错pickling error问题解决方法分析
2019/03/20 Python
python图形界面开发之wxPython树控件使用方法详解
2020/02/24 Python
利于python脚本编写可视化nmap和masscan的方法
2020/12/29 Python
详解CSS3浏览器兼容
2016/12/14 HTML / CSS
HTML5 Canvas 实现K线图的示例代码
2019/12/23 HTML / CSS
Vertbaudet西班牙网上商店:婴儿服装、童装、母婴用品和儿童家具
2019/10/16 全球购物
澳大利亚有机化妆品网上商店:The Well Store
2020/02/20 全球购物
Super-Pharm波兰:药房和香水在一个地方
2020/08/18 全球购物
关联、聚合(Aggregation)以及组合(Composition)的区别
2012/02/29 面试题
描述JSP和Servlet的区别、共同点、各自应用的范围
2012/10/02 面试题
化学相关工作求职信
2013/10/02 职场文书
大学生就业推荐信范文
2013/11/29 职场文书
护理人员的自我评价分享
2014/03/15 职场文书
社团活动总结模板
2014/06/30 职场文书
综合测评自我评价
2015/03/06 职场文书
2019经典广告词集锦!
2019/07/02 职场文书
朋友圈早安励志语录!
2019/07/08 职场文书
python cv2图像质量压缩的算法示例
2021/06/04 Python
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
2021/06/22 Python
怎么禁用Win11输入法 最新Win11输入法关闭教程
2022/08/05 数码科技