“数据库被锁定". Qt在SQLite3中出现错误 [英] "Database is locked" error in SQLite3 with Qt

查看:575
本文介绍了“数据库被锁定". Qt在SQLite3中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入式系统和Qt应用程序的x86上使用SQLite3.当多个线程尝试读写数据库时,我遇到了常见错误数据库已锁定".

I'm using SQLite3 on an embedded system and on x86 in a Qt application. I'm experiencing the common error "Database is locked" when multiple threads try to read/write the database.

我阅读了其他答案中建议的这篇文章,但我为每个线程创建一个不同的连接.

I read this article suggested in some other answers, but I'm creating a different connection for each thread.

通过稍微调整QSQLITE_BUSY_TIMEOUT选项(到一个非常大的值:10000000),我在x86系统和嵌入式系统上解决了这个问题,但是在后一种情况下,仅当不使用事务时才解决.不幸的是,我需要在每个线程的所有工作中使用事务.

By adjusting a bit the QSQLITE_BUSY_TIMEOUT option (to a very large value: 10000000) I solved this problem on the x86 system and on the embedded system, but in the latter case only when not using transactions. Unfortunately I need to use transactions for all the work of each thread.

我的问题是:使用事务时,SQLite3是否不支持同时从数据库中进行读写操作?为什么不简单地等待所有必要的时间来获取锁?也许我没有正确设置?

My question is: isn't it supported by SQLite3 to read/write from/to the database concurrently when using transactions? Why doesn't it simply wait all the necessary time to acquire the lock? Maybe I haven't set it up correctly?

推荐答案

阅读开始交易 SQL语句.它明确表示默认事务行为已延迟,这说明了您所看到的错误.另请阅读此

Read BEGIN TRANSACTION statement of SQL. It explicitly says that the default transaction behavior is deferred which explains the error that you are seeing. Also read this link for another good explanation.

因此,您需要以"BEGIN 立即 TRANSACTION"开始SQL,其他所有人都必须这样做.

So you need to start your SQL as "BEGIN IMMEDIATE TRANSACTION"and everybody else must do the same.

您可以找到源代码示例此处 .注意

You can find source code example here. Pay attention to

bool SqlEngine::beginTransaction()

方法,并在您的代码中执行相同的操作.

method and do the same in your code.

这篇关于“数据库被锁定". Qt在SQLite3中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆