【发布时间】:2020-02-22 18:41:14
【问题描述】:
如何在 python 中从 sql 中提取数据? 我有这段代码,但它重复了所有不是我想要的值。 我希望在我的 python 中有新的更新的 sql 数据。我几乎被击中了几天......任何善良的灵魂请 我用的是sqlserver2012,python
import time
sql_conn = connectSQLServer('ODBC Driver 13 for SQL Server', 'DESKTOP-K6A10IO\SQLEXPRESS', 'display')
mycursor = sql_conn.cursor()
a = 0
while True:
try:
time.sleep(1)
a=a+1
print(a)
sql = "SELECT * FROM dbo.LiveStatsFromSQLServer"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
except Exception as error:
print("Nothing in database\n")
print(error)
sql_conn.commit()
The result shown is
1
(1625, 3)
(1626, 0)
(1627, 10)
2
(1625, 3)
(1626, 0)
(1627, 10)
3
(1625, 3)
(1626, 0)
(1627, 10)
(1622, 20)
while i wish to have a constantly updating to the end of the list like:
(1625, 3)
(1626, 0)
(1627, 10)
after 1 second
(1622, 20)
after 1 second
(1612, 10)
【问题讨论】:
-
你能澄清一下问题是什么吗?顺便说一句,像这样使用
except Exception as error:是不好的做法,请参阅stackoverflow.com/questions/54948548/…、stackoverflow.com/questions/4990718/… -
好的,注意到了。我希望有新的数据记录(每 1 个)从 sql 到我的 python,我做不到
标签: python mysql sql flask plotly-dash