当前位置:主页 > 软件编程 > Python代码 >

python 读取数据库并绘图的实例

时间:2021-02-14 11:28:29 | 栏目:Python代码 | 点击:

1.安装相应的库文件

sudo apt-get install python-mysqldb 

2.数据库操作

import MySQLdb 
db = MySQLdb.connect(“localhost”, “root”, “pwd”, “dbname”, charset='utf8' )

数据库的查询

#加入参数表示查询返回的是一个键值数组而不是默认的元组 
cursor = db.cursor(cursorclass = MySQLdb.cursors.DictCursor)

try:

# 执行SQL语句 
cursor.execute(sql) 
# 获取所有记录列表 
results = cursor.fetchall() 
for row in results: 
fname = row[‘key'] 
# 打印结果 
print fname 
except: 
print “Error: unable to fecth data” 
db.close()

您可能感兴趣的文章:

相关文章