7/20/2023 0 Comments Sqlitestudio commit changesIt is useless anyway because what you want here is a timer, not a thread. So it is a good idea to commit systematically. If you just close your databaseĬonnection without calling commit() first, your changes will be lost! Note that this does notĪutomatically call commit(). Or better yet, just do that commit in the finally block.īy the way the doc says this about the close function: Fix: add an exception handler that does some cleanup, commits and closes the DB. If your program crashes for any reason, your routine may not be triggered. One example: in this code there is no exception handling. Worth reading too: How to improve SQLite insert performance in Python 3.6? With a timer it would still periodically call a function (and notice there's nothing to do). If there is no INSERT during 60 minutes, with my method it won't do anything at all. Note: this method is not "Do it every 10 seconds" (this would be a classic timer), but rather: "Do it 10 seconds later if another INSERT comes in the meantime, do all of them together". If you reopen multiple times, less than 10 seconds later, you will see that it will be grouped in the same commit, as desired. If you run this code, opening once will plan a commit 10 seconds later. Time.sleep(10) # I hope this doesn't block/waste CPU here?ĭb = nnect('test.db', check_same_thread=False)ĭb.execute("CREATE TABLE IF NOT EXISTS test (a int)") import bottle, sqlite3, random, threading, index():Ĭ.execute('INSERT INTO test VALUES (?)', (random.randint(0, 10000),))Ĭommitthread = threading.Thread(target=commit) I'll do this, but this part is not really on topic here). (Note: I know that the use of a global variable should be avoided, and replaced by a class / object with attributes, etc. What do you think of the following code? Is it safe to do like this? I finally came to this solution, which is more or less a "debounce"-like method: if multiple SQL INSERT happen during a 10-second timeframe, group all of them in a single DB commit. Thus I wanted to improve this in How to commit DB changes in a Flask or Bottle app efficiently? In a Python Bottle server using SQLite, I noticed that doing a DB commit after each INSERT is not efficient: it can use 100ms after each client request.
0 Comments
Leave a Reply. |