我需要从MySQL工作台获取一些数据到Python中,这样我才能制作图表。 但是当我尝试插入它并尝试打印值时,这是使用pymysql(参见代码)。它运行得很慢。
所以我想当我在做实时图表和更多东西时,程序会冻结......
有人有解决方案吗?
我的代码:
import pymysql
conn= pymysql.connect(host='127.0.0.1',
port= 3306,
user= 'root',
passwd= '******',
db= 'energie'
)
a = conn.cursor()
sql1 = 'SELECT `VarValue`, `VarName` from `gasmeter1`;'
a.execute(sql1)
gasmeter1 = a.fetchone()
sql2 = 'SELECT `VarValue` from `gasmeter2`;'
a.execute(sql2)
gasmeter2 = a.fetchone()
sql3 = 'SELECT `VarValue` from `gasmeter349`;'
a.execute(sql3)
gasmeter349 = a.fetchone()
sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)
Binnentemperatuur = a.fetchone()
sql5 = 'SELECT `VarValue` from `buiten temp241`;'
a.execute(sql5)
Buitentemperatuur = a.fetchone()
print (gasmeter1[0] , gasmeter2[0] , gasmeter349[0] , Buitentemperatuur[0] , Binnentemperatuur[0])
这只是一个简单的代码,但速度很慢。有人建议吗?
时间:
代码:
import pymysql
import time
conn= pymysql.connect(host='127.0.0.1',
port= 3306,
user= 'root',
passwd= 'ohdecg',
db= 'energie'
)
a = conn.cursor()
t1 = time.time()
sql1 = 'SELECT `VarValue` from `gasmeter1`;'
a.execute(sql1)
gasmeter1 = a.fetchone ()
sql2 = 'SELECT `VarValue` from `gasmeter2`;'
a.execute(sql2)
gasmeter2 = a.fetchone ()
sql3 = 'SELECT `VarValue` from `gasmeter349`;'
a.execute(sql3)
gasmeter349 = a.fetchone ()
sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)
Binnentemperatuur = a.fetchone()
sql5 = 'SELECT `VarValue` from `buiten temp241`;'
a.execute(sql5)
Buitentemperatuur = a.fetchone()
t2 = time.time()
print("The SQL statement took : {} s".format(t2 - t1))
#print (gasmeter1[0] , gasmeter2[0], gasmeter349[0])
>>>
========================= RESTART: C:\Python27\j.py =========================
The SQL statement took : 32.0269999504 s
>>>
答案 0 :(得分:0)
你的桌子有多大?你每次都要检索整个表,然后只取第一个元素。你可以尝试用这样的东西计时:
import time
...
t1 = time.time()
sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)
t2 = time.time()
print("The SQL statement took : {} s".format(t2 - t1))