当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当前回答
我在一个场景中得到了这个错误,与这里描述的略有不同。
SQLite数据库位于由3个服务器共享的NFS文件系统上。在其中2个服务器上,我能够成功地对数据库运行查询,在第三个服务器上,我认为我得到了“数据库已锁定”消息。
第三台机器的问题是,在/var上没有剩余空间了。每次我试图在位于这个文件系统的任何SQLite数据库中运行查询时,我都得到了“数据库被锁定”的消息,并且在日志上出现了这个错误:
Aug 8 10:33:38 server01 kernel: lockd: cannot monitor 172.22.84.87
还有这个:
Aug 8 10:33:38 server01 rpc。statd[7430]: Failed to insert: writing /var/lib/nfs/statd/sm/other.server.name.com: No space left on device .日志含义 Aug 8 10:33:38 server01 rpc。statd[7430]: STAT_FAIL to server01 for SM_MON of 172.22.84.87
太空的情况处理好之后,一切都恢复正常了。
其他回答
删除-journal文件听起来是个糟糕的主意。它允许sqlite在崩溃后将数据库回滚到一致的状态。如果在数据库处于不一致状态时删除它,则会留下一个损坏的数据库。引用sqlite站点的一个页面:
If a crash or power loss does occur and a hot journal is left on the disk, it is essential that the original database file and the hot journal remain on disk with their original names until the database file is opened by another SQLite process and rolled back. [...] We suspect that a common failure mode for SQLite recovery happens like this: A power failure occurs. After power is restored, a well-meaning user or system administrator begins looking around on the disk for damage. They see their database file named "important.data". This file is perhaps familiar to them. But after the crash, there is also a hot journal named "important.data-journal". The user then deletes the hot journal, thinking that they are helping to cleanup the system. We know of no way to prevent this other than user education.
The rollback is supposed to happen automatically the next time the database is opened, but it will fail if the process can't lock the database. As others have said, one possible reason for this is that another process currently has it open. Another possibility is a stale NFS lock, if the database is on an NFS volume. In that case, a workaround is to replace the database file with a fresh copy that isn't locked on the NFS server (mv database.db original.db; cp original.db database.db). Note that the sqlite FAQ recommends caution regarding concurrent access to databases on NFS volumes, because of buggy implementations of NFS file locking.
我无法解释为什么删除一个-journal文件会让你锁定一个数据库,而你以前不能。这是可复制的吗?
顺便说一下,-journal文件的存在并不一定意味着发生了崩溃或有要回滚的更改。Sqlite有几种不同的日志模式,在PERSIST或TRUNCATE模式下,它始终保留-journal文件,并更改内容以指示是否有要回滚的部分事务。
我在多线程应用程序中也有“数据库被锁定”错误,这似乎是SQLITE_BUSY结果代码,我通过将sqlite3_busy_timeout设置为适当的长度(如30000)来解决这个问题。
(顺便说一句,对于一个7年前的问题,居然没有人发现这一点,这真奇怪!SQLite真的是一个奇特而神奇的项目…)
我在Mac OS X 10.5.7上从终端会话运行Python脚本时遇到了同样的问题。尽管我已经停止了脚本,并且终端窗口位于命令提示符处,但它在下次运行时仍然会给出这个错误。解决方案是关闭终端窗口,然后再次打开它。我觉得没道理,但奏效了。
如果一个进程在SQLite DB上有一个锁,并且崩溃了,那么这个DB将永远处于锁定状态。这就是问题所在。并不是其他进程有锁。
我在谷歌Chrome浏览器中查看存储的密码时遇到了这个错误。
# ~/.config/google-chrome/Default
$ sqlite3 Login\ Data
SQLite version 3.35.5 2021-04-19 18:32:05
sqlite> .tables
Error: database is locked
如果你不是特别关心父进程,或者你不想停止当前正在使用数据库的chrome进程,只需将文件复制到其他地方。
$ cp Login\ Data ~/tmp/ld.sql
$ sqlite3 ~/tmp/ld.sql .tables
field_info meta sync_model_metadata
insecure_credentials stats
logins sync_entities_metadata
这样做将允许您读取数据库的内容,而不打扰或停止主chrome进程。