Python查询Mysql时返回字典结构的代码
来源:互联网 发布:淘宝手办哪家比较好 编辑:程序博客网 时间:2024/05/16 17:45
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)
默认程序:
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查询Mysql时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码
- python mysqldb 查询返回字典结构
- Python MySQLdb 查询返回字典结构
- Python MySQLdb 查询返回字典结构
- Python中让MySQL查询结果返回字典类型的方法
- python3 MySQL 查询返回字典类型
- python查询mysql,返回json
- django 查询返回字典
- python字典结构get()方法的陷阱
- Python的代码结构
- JAVA连接MYSQL通过查询返回的结果集获取表结构字段类型
- Python 字典结构遍历
- Python 二维字典查询
- 查询mysql的表结构
- mysql数据字典代码
- matlab中结构体和python中字典的异同
- Python使用Socket(Https)Post登录百度的实现代码
- hdu 4433 字符串变化 DP
- python 实现插入排序算法
- python 实现堆排序算法代码
- Android之SharedPreferences权限
- Python查询Mysql时返回字典结构的代码
- sqlite3+使用总结
- Python正则表达式介绍
- 带命令行参数的main函数详解
- CakePHP 2.x CookBook 中文版 第一章 欢迎
- java设计模式之适配器模式
- hadoop是由mapreduce和hdfs组成的
- Python 代码性能优化技巧分享
- 新闻集团高群耀:移动APP统治下一个十年