Python中让MySQL查询结果返回字典类型的方法
2018-04-11 07:06
801 查看
Python的MySQLdb模块是Python连接MySQL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据为tuple类型。
我们可以这么干使得数据查询结果返回字典类型,即 字段=数据
导入模块
import MySQLdb.cursors
完整代码:
import MySQLdb
import MySQLdb.cursors
class mydb(object):
def __init__(self, host,name,pw,dbname):
try:
#cursorclass = MySQLdb.cursors.DictCursor 返回的数据是字典类型 而不是元祖
self.conn = MySQLdb.connect(host,name,pw,dbname,charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)
except MySQLdb,e:
print e
我们可以这么干使得数据查询结果返回字典类型,即 字段=数据
导入模块
import MySQLdb.cursors
完整代码:
import MySQLdb
import MySQLdb.cursors
class mydb(object):
def __init__(self, host,name,pw,dbname):
try:
#cursorclass = MySQLdb.cursors.DictCursor 返回的数据是字典类型 而不是元祖
self.conn = MySQLdb.connect(host,name,pw,dbname,charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)
except MySQLdb,e:
print e
相关文章推荐
- Python中让MySQL查询结果返回字典类型的方法
- Python中让MySQL查询结果返回字典类型的方法
- Python中让MySQL查询结果返回字典类型的方法
- 用python操作mysql数据库(之数据查询结果返回字典类型)
- python3 MySQL 查询返回字典类型
- Python Sqlite3以字典形式返回查询结果的实现方法
- Python Sqlite3以字典形式返回查询结果的实现方法
- MYSQL存储过程查询返回结果处理方法
- Python查询Mysql时返回字典结构的代码
- Python查询Mysql时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码
- Python查询Mysql时返回字典结构的代码
- [mysql]当mysql查询语句查询的结果为空时,返回query结果是什么类型的呢?
- python mysql 查询返回字典结构
- mysql查询数据返回touple改为字典的方法
- 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
- Python查询Mysql, sqlite时返回字典结构的代码
- python连接mysql查询数据返回字典格式
- MySQL中数据结果集分页功能的实现方法;数据库查询返回特定结果即分页查询
- Python查询Mysql时返回字典结构的代码