The most likely reason you get errors with locked databases is because you should specify
conn.commit()
after the database operation is completed. If you do not, your database will be locked for writing and will remain so. Other threads that are waiting for recording will time out after some time (the default value is 5 seconds, see http://docs.python.org/2/library/sqlite3.html#sqlite3.connect for details on this).
An example of a correct and parallel insert is the following:
import threading, sqlite3 class InsertionThread(threading.Thread): def __init__(self, number): super(InsertionThread, self).__init__() self.number = number def run(self): conn = sqlite3.connect('yourdb.db', timeout=5) conn.execute('CREATE TABLE IF NOT EXISTS threadcount (threadnum, count);') conn.commit() for i in range(1000): conn.execute("INSERT INTO threadcount VALUES (?, ?);", (self.number, i)) conn.commit()
If you like SQLite or other tools that work with SQLite databases, or you want to replace CSV files with SQLite db files, or need to do something as rare as cross-platform IPC, then SQLite is a great tool and very suitable for the purpose. Do not allow yourself to put pressure on the use of another solution if it does not seem right!
shezi Nov 08 '13 at 13:33
source share