让python的mysqldb返回的结果集为字典类型
来源:互联网 发布:按键精灵 数组直接赋值 编辑:程序博客网 时间:2024/06/16 06:13
默认mysqldb返回的是元组,这样对使用者不太友好,也不利于维护
下面是解决方法
import MySQLdbimport MySQLdb . cursors conn = MySQLdb . Connect ( host = ' localhost ' , user = ' root ' , passwd = '' , db = ' test ' , compress = 1 , cursorclass = MySQLdb . cursors . DictCursor ) # <- importantcursor = conn . cursor ()cursor . execute ( " SELECT name, sometext txt FROM foo " )rows = cursor . fetchall ()cursor . close ()conn . close () for row in rows : print row [ ' name ' ] , row [ ' txt ' ] # bingo! # another (even better) way is: conn = MySQLdb . Connect ( host = ' localhost ' , user = ' root ' , passwd = '' , db = ' test ' , compress = 1 )cursor = conn . cursor ( cursorclass = MySQLdb . cursors . DictCursor )# ...# results by field namecursor = conn . cursor ()# ...# ...results by field number
http://blog.csdn.net/suofiya2008/article/details/5690506
0 0
- 让python的mysqldb返回的结果集为字典类型
- 让python的mysqldb返回的结果集为字典类型
- Python中让MySQL查询结果返回字典类型的方法
- [MySQLdb模块]如何让游标cursor的fetchall函数返回字典,而不是元组
- python mysqldb 查询返回字典结构
- Python MySQLdb 查询返回字典结构
- Python MySQLdb 查询返回字典结构
- stuts2的action与返回结果为json类型配置
- oracle 返回table类型的结果集:
- struts2返回结果的类型
- Python mysqlDB 掉线自动连接 返回插入字典
- 怎样让Oracle的存储过程返回结果集
- 怎样让Oracle的存储过程返回结果集
- python3的 pymysql把mysqldb库取代了,让python 3支持mysqldb的解决方法
- Python 对字典元素的赋值 (字典key和value均为组合类型)
- python的MySqldb使用
- python mysqldb的编写
- mybatis查询的返回类型为基础类型(int、long等),但结果为null时的异常解决
- Activity.startManagingCursor方法
- caoliu,caoliushequ,cl社区,1024论坛,1024cl社区最新地址
- 堆和栈:C++中系统内存分区与程序内存分区
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
- chapter7 机器学习之元算法(adaboost)提高分类性能从原理到实现
- 让python的mysqldb返回的结果集为字典类型
- 更换背景——从相册或相机中获取图片并截取
- AlphabetIndexer字母索引辅助类
- 随机化算法——随机数类代码
- 夏时制
- C++中系统内存分区与程序内存分区
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- java中的io系统详解(值得一看)
- 告诉你掌握哪些才算掌握了Java基础