版本:python3.6
相关安装库已装好:plotly,pymysql
代码如下:
import pymysql
db = pymysql.connect(host='',user='',passwd='',db='text',port=3306,charset='utf8')
cursor = db.cursor()
data = cursor.execute('SELECT * FROM ABC
')
all = cursor.fetchall()
顺利连接,并用fetchall()获取所有数据为(('18', '125', '285'), ('11', '178', '235'), ('10', '154', '254'), ('9', '124', '301'), ('18', '177', '256'), ('15', '152', '245'), ('12', '145', '234'), ('12', '167', '254'), ('17', '111', '289'), ('15', '187', '245'))分三列,列名分别为A,B,C
下面我就不知道咋操作了,试了好多种方法报错
还请大神指点迷津,最好测试能跑。
建议你使用pandas这个库来提取sql数据
import pandas as pd
import pymysql
conn = pymysql.connect(host=127.0.0.1'', port=3306, user='root', passwd='123456', db='mysql')
sql = "select * from user"
df = pd.read_sql(sql, conn)
print(df) ##整个数据库的表格
A=df['A'].values.tolist() ##A列数据
print(A)
然后在去plotly绘图就可以了
dataset = {'x':A,
'y': B,
'text':C}
data_g = []
tr_x = Scatter(
x=dataset['x'],
y=dataset['y'],
text=dataset['text'],
textposition='top center',
mode='markers+text',
name='y'
)
data_g.append(tr_x)
layout = Layout(title="scatter plots", xaxis={'title': 'x'}, yaxis={'title': 'value'})
fig = Figure(data=data_g, layout=layout)
pltoff.plot(fig, filename=name)
https://blog.csdn.net/chengxuyuanyonghu/article/details/55519382
https://www.cnblogs.com/felixwang2/p/8903642.html