转换mysql 结果集为词典类型_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1929 | 回复: 0   主题: 转换mysql 结果集为词典类型        下一篇 
huan.liu
注册用户
等级:新兵
经验:72
发帖:58
精华:0
注册:2011-11-23
状态:离线
发送短消息息给huan.liu 加好友    发送短消息息给huan.liu 发消息
发表于: IP:您无权察看 2015-7-30 11:08:36 | [全部帖] [楼主帖] 楼主

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 :  <type 'tuple'>

(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 :   <type 'tuple'>

{'Value': u'6491620', 'Variable_name': u'Com_delete'}

----------------------------------------

使用 dict(result)将结果集转换为词典,得到如下结果:

the type of mystat2 is :  <type 'dict'>

{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()

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论