网络编程
位置:首页>> 网络编程>> 数据库>> python 读取数据库并绘图的实例

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

作者:ALWAYS_FANG  发布时间:2024-01-27 10:43:33 

标签: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()

来源:https://blog.csdn.net/qq_38120760/article/details/81531762

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com