用python操作mysql数据库(之数据查询结果返回字典类型)
#!/usr/bin/envpython#-*-coding:utf-8-*-importMySQLdb#建立连接conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='1qaz#EDC',db='test_db')cur=conn.cursor(MySQLdb.cursors.Dic...
·
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
#建立连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1qaz#EDC',db='test_db')
cur = conn.cursor(MySQLdb.cursors.DictCursor) #建立游标时,加上"MySQLdb.cursors.DictCursor",让数据查询结果返回字典类型
#对数据进行操作
sql = "select * from user" #定义查询操作的sql语句
cur.execute(sql) #执行sql语句
data = cur.fetchall() #读取数据
#关闭数据库连接
cur.close()
conn.close()
print data
转载于:https://blog.51cto.com/freshair/1876291
更多推荐
已为社区贡献2条内容
所有评论(0)