您的位置:首页 > 编程语言 > Python开发

用python操作mysql数据库(之数据查询结果返回字典类型)

2016-11-24 17:04 801 查看
#!/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python