Python中让MySQL查询结果返回字典类型的方法
2018-01-15 10:31
1031 查看
import pymysql host='localhost' user='root' passwd='root' port=3306 db='test' db=pymysql.connect( host=host, user=user, passwd=passwd, db=db, port=port, charset='utf8', cursorclass = pymysql.cursors.DictCursor ) cursor=db.cursor() sql="select * from documents" try: cursor.execute(sql) result=cursor.fetchall() for i in result: for j in i: print(j,"=>",i[j]) print(" ") except: print('error') db.close()
在默认情况下cursor方法返回的是BaseCursor类型对象,BaseCursor类型对象在执行查询后每条记录的结果以列表(list)表示。如果要返回字典(dict)表示的记录,就要设置cursorclass参数为MySQLdb.cursors.DictCursor类。
相关文章推荐
- Python中让MySQL查询结果返回字典类型的方法
- Python中让MySQL查询结果返回字典类型的方法
- Python中让MySQL查询结果返回字典类型的方法
- python3 MySQL 查询返回字典类型
- 用python操作mysql数据库(之数据查询结果返回字典类型)
- Python Sqlite3以字典形式返回查询结果的实现方法
- Python Sqlite3以字典形式返回查询结果的实现方法
- MYSQL存储过程查询返回结果处理方法
- Python查询Mysql时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码
- Python查询Mysql时返回字典结构的代码
- [mysql]当mysql查询语句查询的结果为空时,返回query结果是什么类型的呢?
- python mysql 查询返回字典结构
- Python查询Mysql时返回字典结构的代码
- mysql查询数据返回touple改为字典的方法
- python连接mysql查询数据返回字典格式
- 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
- Python查询Mysql, sqlite时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码
- MySQL中数据结果集分页功能的实现方法;数据库查询返回特定结果即分页查询