Reputation: 97
I am working on a QT C++ application which has a sqlite database. The tables are displayed using QTableView and QSqlTableModel. There are tables with around 10K records.
My issue is that when I try to update any record into a table with 10K records, I get the error as "Database is Locked, Unable to fetch row". This doesnt happen when the row count is less(say 20). The journal file is created in the applications folder. Seems some process is holding a lock onto database. Can't figure out the actual cause.
Can anyone suggest some solution?
Thanks, Priyanka
Upvotes: 4
Views: 10267
Reputation: 51
Check to see if you have the sqlite database open in another window. I had the same issue but then noticed I had unsaved changes in another open window on the database. All worked perfectly once that instance was closed.
Upvotes: 5
Reputation: 73
In Qt, you send a PRAGMA to your database like this:
dbObj = QSqlDatabase::addDatabase(...);
dbObj.setDatabaseName(...);
dbObj.open();
dbObj.exec("PRAGMA locking_mode = EXCLUSIVE");
However, I don't think that is what you want. From the Qt documentation:
The driver is locked for updates while a select is executed. This may cause problems when using QSqlTableModel because Qt's item views fetch data as needed (with SqlQuery::fetchMore() in the case of QSqlTableModel).
Take a look at QSqlQuery::isActive which says:
Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully but not yet finished with. When you are finished with an active query, you can make make the query inactive by calling finish() or clear(), or you can delete the QSqlQuery instance.
The bottom line is that you have a blocking query originating from somewhere that you either need to properly make "inactive" or that you'll need to arbitrate with.
Upvotes: 6