Python中让MySQL查询结果返回字典类型的方法


Posted in Python onAugust 22, 2014

Python的MySQLdb模块是Python连接MySQL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据
默认连接数据库:

MySQLdb.connect(

    host=host,

        user=user,

        passwd=passwd,

        db=db,

        port=port,

        charset='utf8'

)

查询数据:
cur = conn.cursor()

cur.execute('select b_id from blog limit 1')

data = cur.fetchall()  

cur.close()

conn.close()

打印:
for row in data:

    print type(row)

    print row

执行结果:
<type 'tuple'>

(1L,)

为tuple类型。
我们可以这么干使得数据查询结果返回字典类型,即 字段=数据
导入模块
import MySQLdb.cursors

在连接函数里加上这个参数  cursorclass = MySQLdb.cursors.DictCursor 如:
MySQLdb.connect(

    host=host,

        user=user,

        passwd=passwd,

        db=db,

        port=port,

        charset='utf8',

    cursorclass = MySQLdb.cursors.DictCursor

)

再重新运行脚本,看看执行结果:
<type 'dict'>

{'b_id': 1L}

搞定!
注意,在连接的时候port如果要指定则值必须是整型,否则会出错!
Python 相关文章推荐
Python实现批量下载图片的方法
Jul 08 Python
python 中if else 语句的作用及示例代码
Mar 05 Python
PyQt5实现无边框窗口的标题拖动和窗口缩放
Apr 19 Python
python内置数据类型之列表操作
Nov 12 Python
Python的Lambda函数用法详解
Sep 03 Python
PyTorch里面的torch.nn.Parameter()详解
Jan 03 Python
pytorch构建多模型实例
Jan 15 Python
如何通过python实现人脸识别验证
Jan 17 Python
pandas分组聚合详解
Apr 10 Python
Pycharm插件(Grep Console)自定义规则输出颜色日志的方法
May 27 Python
Python urllib库如何添加headers过程解析
Oct 05 Python
用python对excel进行操作(读,写,修改)
Dec 25 Python
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
Aug 22 #Python
Python with的用法
Aug 22 #Python
Tornado服务器中绑定域名、虚拟主机的方法
Aug 22 #Python
python with statement 进行文件操作指南
Aug 22 #Python
Python中还原JavaScript的escape函数编码后字符串的方法
Aug 22 #Python
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Aug 22 #Python
Python升级提示Tkinter模块找不到的解决方法
Aug 22 #Python
You might like
默默简单的写了一个模板引擎
2007/01/02 PHP
新手配置 PHP 调试环境(IIS+PHP+MYSQL)
2007/01/10 PHP
javascript 一个自定义长度的文本自动换行的函数
2007/08/19 Javascript
关于JS控制代码暂停的实现方法分享
2012/10/11 Javascript
js 字符串转换成数字的三种方法
2013/03/23 Javascript
ie下$.getJSON出现问题的解决方法
2014/02/12 Javascript
jQuery获取(选中)单选,复选框,下拉框中的值
2014/02/21 Javascript
ext中store.load跟store.reload的区别示例介绍
2014/06/17 Javascript
swtich/if...else的替代语句
2015/08/16 Javascript
Bootstrap入门书籍之(三)栅格系统
2016/02/17 Javascript
在vue项目中引入highcharts图表的方法(详解)
2018/03/05 Javascript
小程序登录/注册页面设计的实现代码
2019/05/24 Javascript
vue中英文切换实例代码
2020/01/21 Javascript
在vscode 中设置 vue模板内容的方法
2020/09/02 Javascript
[02:38]2018年度DOTA2最佳劣单位选手-完美盛典
2018/12/17 DOTA
Python从MP3文件获取id3的方法
2015/06/15 Python
详解python中的json和字典dict
2018/06/22 Python
详解python tkinter教程-事件绑定
2019/03/28 Python
Django框架静态文件使用/中间件/禁用ip功能实例详解
2019/07/22 Python
Python PO设计模式的具体使用
2019/08/16 Python
django中的数据库迁移的实现
2020/03/16 Python
基于virtualenv创建python虚拟环境过程图解
2020/03/30 Python
解决pyPdf和pyPdf2在合并pdf时出现异常的问题
2020/04/03 Python
Lime Crime官网:美国一家主打梦幻精灵系的彩妆品牌
2019/03/22 全球购物
什么是方法的重载
2013/06/24 面试题
高一生物教学反思
2014/01/17 职场文书
个性与发展自我评价
2014/02/11 职场文书
软件毕业生个人鉴定
2014/03/03 职场文书
党员干部承诺书
2014/03/25 职场文书
主管竞聘书范文
2014/03/31 职场文书
销售员态度差检讨书
2014/10/26 职场文书
2015年车间主任工作总结
2015/05/21 职场文书
2015毕业设计工作总结
2015/07/24 职场文书
公务员爱岗敬业心得体会
2016/01/25 职场文书
送给客户微信问候语!
2019/07/04 职场文书
浅谈Java父子类加载顺序
2021/08/04 Java/Android