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

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)

Python查询Mysql时返回字典结构的代码的更多相关文章

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

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

  2. [python] 查询mysql返回datetime类型数据的处理

    Python 查询Mysql,如果是datetime类型,在json序列化的时候会出现问题. 在网上查了一下,解决方案基本都是遍历dict数据,如果是datetime则转化为字符串. from dat ...

  3. 解决Python查询Mysql中文乱码问题

    前段时间,自己瞎动手用Django写了一个更新zip包和sql到远程服务器的工具.但Python从Mysql中读取出来的中文字符会乱码,如下图: 解决办法:Python连接Mysql时指定charse ...

  4. python查询mysql数据(3)

    python查询mysql数据(3) """数据查询""" import pymysql import datetime from pymy ...

  5. python连mysql时避免出现乱码

    使用python连mysql时候,常常出现乱码,采取以下措施可以避免 1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)2 MySQL数据库charset=ut ...

  6. python连接mysql时连接不到test文件夹怎么办

    最新版mysql安装后默认是没有test文件夹的,这时候需要我们自己创建一个test文件夹, 文件默认路径如下    C:\ProgramData\MySQL\MySQL Server 5.7\Dat ...

  7. python查询mysql以字典返回

    # *_*coding:utf-8 *_* import pymysql conn = pymysql.connect(host='192.168.33.10', user='root', passw ...

  8. python 调用mysql存储过程返回结果集

    存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...

  9. Python查询MySQL进行远程采集图片实例

    这是四五年以前做小说站采集图片时写过唯一一次 Python 代码 #!/usr/bin/python #-*-coding:utf-8-*- import MySQLdb, os, socket, t ...

随机推荐

  1. 推荐排序---Learning to Rank:从 pointwise 和 pairwise 到 listwise,经典模型与优缺点

    转载:https://blog.csdn.net/lipengcn/article/details/80373744 Ranking 是信息检索领域的基本问题,也是搜索引擎背后的重要组成模块. 本文将 ...

  2. 【python】发送邮件,含附件

    def send_mail(_user,_pwd,_to): # f = open(file_new,'rb') # mail_body = f.read() # f.close() # 读取最新测试 ...

  3. .Nginx安装filebeat收集日志:

    1.安装filebeat: [root@nginx ~]# vim /usr/local/filebeat/filebeat.yml [root@nginx ~]# tar xf filebeat-6 ...

  4. python_字符串方法

    1.字符串大小写转 value = "wangdianchao" # 转换为大写 big_value = value.upper() print(big_value) # 转换为小 ...

  5. [唐胡璐]Java操作Sql Server 2008数据库

    下载Microsoft JDBC Driver for SQL Server 直接去官网下载即可: 下载解压文件,得到sqljdbc.jar和sqljdbc4.jar。如果你使用的是jre1.7版本, ...

  6. 关键字local、global和内置函数【locals、globals】

    每个函数都有着自已的命名空间,叫做局部名字空间,它记录了函数的变量,包括函数的参数和局部定义的变量.每个模块拥有它自已的命名空间,叫做全局命名空间,它记录了模块的变量,包括函数.类.其它导入的模块.模 ...

  7. [2019牛客多校第三场][G. Removing Stones]

    题目链接:https://ac.nowcoder.com/acm/contest/883/G 题目大意:有\(n\)堆石头,每堆有\(a_i\)个,每次可以选其中两堆非零的石堆,各取走一个石子,当所有 ...

  8. Codeforces Round #584 A. Paint the Numbers

    链接: https://codeforces.com/contest/1209/problem/A 题意: You are given a sequence of integers a1,a2,-,a ...

  9. 父元素设置固定宽度并设置overflow:scroll,如何让子元素撑开父元素

    <div class="a"> <div class="b"> <div class="c">内容内容, ...

  10. MySQL数据库有几种索引?分别是什么?

        5种索引     1.主键索引     2.唯一索引     3.普通索引     4.全文索引     5.联合索引