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()
上一篇:Python进阶之使用selenium爬取淘宝商品信息功能示例
栏 目:Python代码
下一篇:在Linux命令行终端中使用python的简单方法(推荐)
本文标题:python 读取数据库并绘图的实例
本文地址:http://www.codeinn.net/misctech/62549.html






