[PyQt] QSqlTableModel in ReadOnly mode concurrent with sqlite3 writer thread

Gelonida N gelonida at gmail.com
Tue Mar 11 17:26:26 GMT 2014


Hi,

I have an application with two threads.
One thread tries to update an sqlite data base
another thread (the GUI thread) tries to visualize the same sqlite DB.


The thread that populates the data / updates the db is using the sqlite3 
module of python.

The GUI is using QSqlTableModel to visualize the data.


My problem is following:
  Sometimes (quite often) I get following error message from the thread 
trying to populate the DB:
> sqlite3.OperationalError: database is locked

As soon as I create the model (whether displaying the related widget or 
not) I encounter "the database is locked" error
>         db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
>         db.setDatabaseName("test.sqlite3")
>         db.setConnectOptions("QSQLITE_OPEN_READONLY=1")
>         db.open()
>         model = QtSql.QSqlTableModel()
>         model.setTable("test")
>         model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
>         model.select()
 >         self.db = db
 >         self.model = model # IF I REMOVE THIS LINE NO LOCKING OCCURS

removing the last line will autodestroy the model and then my other 
thread has no issues.


I read a little about sqlite3 and multithreading and came to the 
conclusion that things should work as long as one thread only modifes 
data and the other ones just reads and as long as every thread uses its 
own connection.

Any suggestions of how I can debug / fix my problem?






More information about the PyQt mailing list