#!/usr/bin/env python # -*- coding: utf-8 -*- import Mysqldb #建立连接 conn&
#!/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
--结束END--
本文标题: 用python操作mysql数据库(之数据查询结果返回字典类型)
本文链接: https://www.lsjlt.com/news/36899.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0