来源:http://blog.csdn.net/zgl_dm/article/details/8710371

默认mysqldb返回的是元组,这样对使用者不太友好,也不利于维护
下面是解决方法

import MySQLdb
import MySQLdb.cursors conn = MySQLdb.Connect (
host = 'localhost', user = 'root' ,
passwd = '', db = 'test', compress = 1,
cursorclass = MySQLdb.cursors.DictCursor, charset='utf8') // <- important
cursor = conn.cursor()
cursor.execute ("SELECT name, txt FROM table")
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 name
cursor = conn.cursor()
# ...
# ...results by field number

注意:如果同时安装了PyMySQL和MySQL-python库,会导致MySQL-python失效,只有pymsql可用

解决办法:

try:
import pymysql as MySQLdb
except ImportError:
import MySQLdb

这样就可以用一套代码搞定了

【mysql】MySQLdb返回字典方法的更多相关文章

  1. Python查询Mysql时返回字典结构的代码

    Python查询Mysql时返回字典结构的代码 MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.D ...

  2. python mysql 查询返回字典结构

    cur = self.conn.cursor(MySQLdb.cursors.DictCursor)加上MySQLdb.cursors.DictCursor可以返回字典结构 {列名:值} class ...

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

    import pymysql host='localhost' user='root' passwd='root' port=3306 db='test' db=pymysql.connect( ho ...

  4. python操作mysql数据-执行语句返回值直接返回字典类型

    fetchall()将结果放在二维数组里面,每一行的结果在元组里面 import pymysql def export(table_name): conn =pymysql.connect(host ...

  5. OpenLayers添加点【php请求MySQL数据库返回GeoJSON数据】

    php请求MySQL数据库返回GeoJSON数据的实现方法请参见: http://www.cnblogs.com/marost/p/6234514.html OpenLayers[v3.19.1-di ...

  6. php mysql 中文乱码解决方法

    本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...

  7. Anaconda 安装 Python 库(MySQLdb)的方法

    [已解决]Anaconda 安装 Python 库(MySQLdb)的方法 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 201 ...

  8. Python 字典方法

    访问字典的值 字典中的 键/值 实际上就是一种映射关系,只要知道了 “键”,就肯定知道 “值”. >>> my_dict = dict(name = 'zhangsan',other ...

  9. python调用数据返回字典dict数据的现象2

    python调用数据返回字典dict数据的现象2 思考: 话题1连接:https://www.cnblogs.com/zwgbk/p/10248479.html在打印和添加时候加上内存地址id(),可 ...

随机推荐

  1. js 数组与字符串的相互转化

    数组转字符串:join() 字符串转数组:split('')

  2. Hive记录-Hive介绍(转载)

    1.Hive是什么? Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的 SQL 查询功能,将类 SQL 语句转换为 MapReduce 任务执 ...

  3. HDU - 4901 The Romantic Hero(dp)

    https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标. ...

  4. CodeForces - 327D Block Tower

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. .NET面试题系列(十一)WinDbg、Perfmon

    WinDbg 资料 https://www.cnblogs.com/sheng-jie/p/9503650.html https://www.cnblogs.com/yudongdong/p/9701 ...

  6. EasyUI Combobox 设置默认值

    /** *绑定运营商,设置默认值, 显示CMCC, 传值1 */ $('#operatingId').combobox({ url:'data_url', valueField:'id', textF ...

  7. u-boot移植(十三)---代码修改---裁剪及环境变量 一

    一.内核裁剪 内核的裁剪首先就是修改我们的配置文件,即 include/configs/jz2440.h 文件,里面定义的很多宏,我们也许用不上的就要去掉. /* * (C) Copyright 20 ...

  8. C# this调用构造函数及析构函数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace trai ...

  9. Rancher

    Rancher Docker容器管理平台:图像化管理平台. centos server 10.100.10.10 docker node 10.100.10.15 安装 docker  search ...

  10. 以前的 Delphi版本

                    Delphi 1 Delphi 2 Delphi 3 Delphi 4 Delphi 5 Delphi 6 Delphi 7 Delphi 8 Delphi 2005