python在使用MySQLdb库的时候,如下方法默认获取的cursor的结果集是tuple结构的。

con = MySQLdb.connect('host',port,'username','passwod','db_name','gbk')

curosr = con.cursor()

sql = "select * from test_table"   #test_table : name,age

cusor = cursor.execute(sql)

r = cusor.fetchone()

print r[0]+'\t'+r[1]
cursor.close() con.close()

我们获得r的结果集是元组形式的,这样如果我们以后更改数据表的字段顺序,那么我们必须要修改现有数据库代码,如何才能做到不修改代码呢,很简单,使用DictCursor,这样得到的结果集就是字典形式的了,我们可以用key去获取数据了。

如下是如何获取DictCursor

cursor = con.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor.execute(sql)
r = cursor.fetchone()
print r['name']+'\t'+r['age']

python mysqldb使用dictcursor的更多相关文章

  1. python MySQLdb 对mysql基本操作方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host=',db='host') ...

  2. python MySQLdb pymsql

    参考文档 https://www.python.org/dev/peps/pep-0249/#nextset 本节内容 MySQLdb  pymysql MySQLdb和pymysql分别为Pytho ...

  3. python MySQLdb安装和使用

    MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-py ...

  4. Python MySQLdb 模块使用方法

    import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...

  5. Python MySQLdb 学习总结(转)

    转自http://www.cnblogs.com/coser/archive/2012/01/12/2320741.html 感谢@糖拌咸鱼 任何应用都离不开数据,所以在学习python的时候,当然也 ...

  6. Python MySQLdb在Linux下的快速安装

    在家里windows环境下搞了一次 见   python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...

  7. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  8. Python mysqldb模块

    #!/usr/bin/env python2.7 #-*- coding:utf8 -*- import os import sys import logging import MySQLdb fro ...

  9. cygwin 下安装python MySQLdb

    cygwin 下安装python MySQLdb 1) cygwin 更新 运行 cygwin/setup-x86_64.exe a 输入mysql,选择下面的包安装: libmysqlclient- ...

随机推荐

  1. Windows下载Android源代码

    下载msysgit,安装 官方下载:http://code.google.com/p/msysgit/downloads/list, 打开Git Bash,运行命令 cd D: git clone h ...

  2. android141 360 安装软件管理

                      主界面: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  3. MYSQL-- binlog事件详解

    mysqlbinlog -vvv log.000001 # at # :: server id end_log_pos CRC32 sequence_number= SET @@SESSION.GTI ...

  4. cvsnt 设置用户、修改密码

    忘记密码后,可以用administrator 新建一个用户,使用这个用户的账号. password agent 设置clear password不好使..   cvsnt配置 创建用户1 .下载cvs ...

  5. Redis 脚本

    Redis 脚本使用 Lua 解释器来执行脚本. Reids 2.6 版本通过内嵌支持 Lua 环境.执行脚本的常用命令为 EVAL. 语法 Eval 命令的基本语法如下: redis 127.0.0 ...

  6. Helpers\Request

    Helpers\Request The Helpers\Request class is used for detecting the type of request and retrieving t ...

  7. 小白日记36:kali渗透测试之Web渗透-手动漏洞挖掘(二)-突破身份认证,操作系统任意命令执行漏洞

    手动漏洞挖掘 ###################################################################################### 手动漏洞挖掘 ...

  8. python-其他常用模块

    本节大纲: 模块介绍 time &datetime模块 random shutil shelve xml处理 yaml处理 configparser hashlib subprocess lo ...

  9. 视频播放-VideoVIew,Vitamio

    播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView类来实现的.这个类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器.VideoView的用法和 Med ...

  10. 实用工具推荐(Live Writer)(2015年05月26日)

    1.写博客的实用工具 推荐软件:Live Writer 使用步骤: 1.安装 Live Essential 2011,下载地址:http://explore.live.com/windows-live ...