I am trying to run a SQL statement every minute, but I need to be able to iterate through incoming data as well continuously. So I have made a thread timer that I found with the help of another SO question, but I am receiving the following error when trying to run it. What are some solutions to this?
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 18460 and this is thread id 29296
Here is a sample of the timer code. I am using a update every second here for testing purposes only.
def update():
threading.Timer(1.0, update).start()
cursor.execute("UPDATE users SET timer = timer + 1")
db.commit()
print("Updating all timers")
update()
This should work by putting the connection in the same thread. If you do it every minute I suppose its ok. Making a connection every second seems a bit expensive though.
def update():
threading.Timer(1.0, update).start()
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute("UPDATE users SET timer = timer + 1")
conn.commit()
print("Updating all timers")
update()
To answer your question of 'why'. I think it is because your connection is on the main thread and your commit to the connection is on another. Hope this is correct and I hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.