我認爲,最好的做法是使用單個SQL語句來更新記錄。
因爲,SQLite has no boolean value,您需要設置值(而不是True)。
例如,聲明可能是:
import time
st = "UPDATE reminderFormat SET Reminded = 1 " \
"WHERE FutureTime <= {currentTime}".format(currentTime=time.time())
編輯:演示
這裏是一個演示:
import time
import sqlite3
records = [
(20183740995, 1503330725.0, "testtt", 0),
(20183740995, 1503330732.0, "testtt", 0),
(20183740995, 1503331334.0, "testtt", 0),
(20183740995, 1509999999.0, "testtt", 0),
]
con = sqlite3.connect(":memory:")
# Create the table
con.execute("create table reminderFormat(AuthorID, FutureTime, Message, Reminded)")
# Fill the table
con.executemany("INSERT INTO reminderFormat(AuthorID, FutureTime, Message, Reminded) VALUES (?, ?, ?, ?)", records)
curr_time = time.time()
con.execute("UPDATE reminderFormat SET Reminded = 1 WHERE FutureTime <= ?", (curr_time,))
# Print the table contents
for row in con.execute("SELECT AuthorID, FutureTime, Reminded FROM reminderFormat"):
print(row)
你得到:
(20183740995, 1503330725.0, 1)
(20183740995, 1503330732.0, 1)
(20183740995, 1503331334.0, 1)
(20183740995, 1509999999.0, 0)
請參閱[如何在堆棧溢出文章中格式化SQL表?](https://meta.stackexchange.com/q/96125) –