QT Sqlite无法获取行。数据库已锁定

时间:2022-09-09 11:00:02

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.

我正在开发一个带有sqlite数据库的QT C ++应用程序。使用QTableView和QSqlTableModel显示表。有大约10K记录的表格。

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.

我的问题是,当我尝试将任何记录更新为具有10K记录的表时,我得到的错误是“数据库已锁定,无法获取行”。当行数较少时(例如20),这不会发生。日志文件在applications文件夹中创建。似乎某些进程正在锁定数据库。无法弄清楚实际原因。

Can anyone suggest some solution?

有谁能建议一些解决方案?

Thanks, Priyanka

2 个解决方案

#1


3  

In Qt, you send a PRAGMA to your database like this:

在Qt中,您将PRAGMA发送到您的数据库,如下所示:

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:

但是,我认为这不是你想要的。从Qt文档:

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).

执行选择时,驱动程序将被锁定以进行更新。这可能会在使用QSqlTableModel时导致问题,因为Qt的项目视图会根据需要获取数据(在QSqlTableModel的情况下使用SqlQuery :: fetchMore())。

Take a look at QSqlQuery::isActive which says:

看看QSqlQuery :: isActive,它说:

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.

如果查询处于活动状态,则返回true。一个活跃的QSqlQuery是一个已经成功但尚未完成的exec()。完成活动查询后,可以通过调用finish()或clear()使查询处于非活动状态,或者可以删除QSqlQuery实例。

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.

最重要的是,你有一个阻止查询来自你需要正确地“无效”或你需要仲裁的某个地方。

#2


1  

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.

检查是否在另一个窗口中打开了sqlite数据库。我有同样的问题但后来发现我在数据库的另一个打开的窗口中有未保存的更改。一旦该实例关闭,所有工作都完美无缺。

#1


3  

In Qt, you send a PRAGMA to your database like this:

在Qt中,您将PRAGMA发送到您的数据库,如下所示:

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:

但是,我认为这不是你想要的。从Qt文档:

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).

执行选择时,驱动程序将被锁定以进行更新。这可能会在使用QSqlTableModel时导致问题,因为Qt的项目视图会根据需要获取数据(在QSqlTableModel的情况下使用SqlQuery :: fetchMore())。

Take a look at QSqlQuery::isActive which says:

看看QSqlQuery :: isActive,它说:

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.

如果查询处于活动状态,则返回true。一个活跃的QSqlQuery是一个已经成功但尚未完成的exec()。完成活动查询后,可以通过调用finish()或clear()使查询处于非活动状态,或者可以删除QSqlQuery实例。

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.

最重要的是,你有一个阻止查询来自你需要正确地“无效”或你需要仲裁的某个地方。

#2


1  

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.

检查是否在另一个窗口中打开了sqlite数据库。我有同样的问题但后来发现我在数据库的另一个打开的窗口中有未保存的更改。一旦该实例关闭,所有工作都完美无缺。