当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当前回答
我只是犯了同样的错误。 5分钟后,我发现我没有关闭一个shell巫婆正在使用db。 请关闭它,再试一次;)
其他回答
删除-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中锁的各种状态的文档非常有用。Michael,如果您可以执行读操作,但不能对数据库执行写操作,这意味着某个进程已经获得了数据库上的保留锁,但还没有执行写操作。如果你正在使用SQLite3,有一个叫做PENDING的新锁,不允许更多的进程连接,但现有的连接仍然可以执行读取,所以如果这是问题所在,你应该看看这个锁。
在windows中,您可以尝试这个程序http://www.nirsoft.net/utils/opened_files_view.html,以找出进程正在处理db文件。尝试关闭该程序解锁数据库
在Linux和macOS中,你可以做类似的事情,例如,如果你锁定的文件是development.db:
$ fuser development.db
这个命令将显示哪个进程正在锁定该文件:
> development.db: 5430
只需终止进程……
杀死-9 5430
...您的数据库将被解锁。
从您之前的评论中,您说存在一个-journal文件。
这可能意味着您已经打开和(EXCLUSIVE?)事务,还没有提交数据。是你的程序或者其他进程留下了-journal吗?
重新启动sqlite进程将查看日志文件,清除任何未提交的操作并删除-journal文件。
正如Seun Osewa所说,有时僵尸进程会占用终端中的锁,即使您认为这是不可能的。您的脚本运行,崩溃,然后返回到提示,但是在某个库调用生成了一个僵尸进程,并且该进程拥有锁。
关闭您所在的终端(在OSX上)可能有用。重启就可以了。你可以寻找(例如)没有做任何事情的“python”进程,并杀死它们。