【Python】转换mysql 结果集为词典类型

Python的MySQLdb模块是Python连接MySQL的一个模块,
使用MySQLdb 模块获取mysql中的记录,默认查询结果返回是tuple类型,只能通过0,1等索引下标访问数据。
默认的连接方式:
conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8')
root@alsdb_admin1b # python dict.py 
the type of res1 is :  
(u'Com_delete', u'6491620')
----------------------------------------
使用 
import MySQLdb.cursors
在conn 中加上 cursorclass = MySQLdb.cursors.DictCursor
conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)
返回的结果集仍然是 tuple 类型但是 结果机里面的值已经变为词典类型了,但是这样并不能解决问题
the type of res2 is :  
{'Value': u'6491620', 'Variable_name': u'Com_delete'}
----------------------------------------
使用dict(result) 将结果集转换为词典,得到如下结果:
the type of mystat2 is :  
{u'Com_delete': u'6491620'}
这样可以直接使用mystat2['Com_delete'] 来调用对应的vlaue
dict.py的代码:
#!/usr/bin/env python
#coding=utf-8
import time
import sys
import MySQLdb
import dbconn
import MySQLdb.cursors
def now() :
        #return str('2011-01-31 00:00:00')
        return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )
def log( qps,tps , logs ) :
        f = file( logs , 'a' , 0 )
        f.write( now() + '  ' + str(qps) +'  '+ str(tps) + '\n' )
        f.close()
def main() :
    try: 
      conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8')
    except  MySQLdb.ERROR,e:
      print "Error %d:%s"%(e.args[0],e.args[1])
      exit(1)
    conn.autocommit(True)
    cursor=conn.cursor()
    mystat1={}
    sql = "show global status where Variable_name in ('Com_delete');"
    cursor.execute(sql)
    res1 = cursor.fetchall()
    for row in res1: 
        print "the type of res1 is : ", type(row)
        print row
    conn.close()
    print "----------------------------------------\n" 
    try:
      conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)
    except  MySQLdb.ERROR,e:
      print "Error %d:%s"%(e.args[0],e.args[1])
      exit(1)

    conn.autocommit(True)
    cursor=conn.cursor()
    mystat2={}
    cursor.execute(sql)
    res2 = cursor.fetchall()
    #mystat2=dict(res2)
    for row in res2:
        print "the type of res2 is : ", type(res2)
        print row  
    conn.close()
    print "----------------------------------------\n"
    try:
      conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8')
    except  MySQLdb.ERROR,e:
      print "Error %d:%s"%(e.args[0],e.args[1])
      exit(1)
    conn.autocommit(True)
    cursor=conn.cursor()
    mystat2={}
    cursor.execute(sql)
    res2 = cursor.fetchall()
    mystat2=dict(res2)
    print "the type of mystat2 is : ", type(mystat2)
    print mystat2
    conn.close()
if __name__ == '__main__':
   main()
上一篇:FileStream的读取和写入


下一篇:CentOS 7 - 安装MySQL 5.7