[英]Update variable within a While loop - Python
我直接在數據庫中更新變量(手動)。 但是,While 循環中的代碼永遠不會更新變量,因為它處於循環中,我想知道我應該怎么做才能獲得新值。
也許退出循環並重新打開它,我嘗試了break,但它關閉了腳本。
import sqlite3
def read_data(data, table):
con = sqlite3.connect("settings.db")
cur = con.cursor()
read_data.row = cur.execute(f"SELECT {data} FROM {table}").fetchall()[0][0]
read_data("data", "eyes")
eyes = read_data.row
while True:
read_data("data", "eyes")
eyes_loop = read_data.row
if eyes_loop != eyes:
print("Eyes changed")
break
else:
print("Eyes not changed")
continue
有很多事情不太對勁:
import sqlite3
def read_data(data, table):
con = sqlite3.connect("settings.db")
cur = con.cursor()
data = cur.execute(f"SELECT {data} FROM {table}").fetchall()[0][0]
con.close()
return data
eyes = read_data("data", "eyes")
while True:
eyes_loop = read_data("data", "eyes")
if eyes_loop != eyes:
print("Eyes changed")
break
else:
print("Eyes not changed")
continue
您在此處附加的代碼之前是否有任何形式的“打開文件”? 如果是這樣,您可能需要打開和關閉它才能正確更新和更新。
您使用的 function 需要時間來連接數據庫並來回發送數據。 此代碼異步運行,因此循環運行並運行通過read_data()
以避免這種使用async/await
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.