当前位置:Gxlcms > 数据库问题 > python3 pymysql查询结果包含字段名

python3 pymysql查询结果包含字段名

时间:2021-07-01 10:21:17 帮助过:86人阅读

python2使用MySQLdb模块进行连接mysql数据库进行操作;python3则使用pymysql模块进行连接mysql数据库进行操作;两者在语法上有稍微的差别,其中就包括查询结果包含字段名,具体例子如下:

python2:

import MySQLdb

conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘adu‘)
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

sql = ‘select * from test1‘
reCount = cur.execute(sql)
nRet = cur.fetchall()

cur.close()
conn.close()

print(nRet)
print(reCount)

 

pytnon3:

import pymysql

conn = pymysql.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘adu‘)
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = ‘select * from test1‘
reCount = cur.execute(sql)
nRet = cur.fetchall()

cur.close()
conn.close()

print(nRet)
print(reCount)

python3 pymysql查询结果包含字段名

标签:class   import   fetch   ret   user   int   数据   exec   ctc   

人气教程排行