当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当前回答
在windows中,您可以尝试这个程序http://www.nirsoft.net/utils/opened_files_view.html,以找出进程正在处理db文件。尝试关闭该程序解锁数据库
在Linux和macOS中,你可以做类似的事情,例如,如果你锁定的文件是development.db:
$ fuser development.db
这个命令将显示哪个进程正在锁定该文件:
> development.db: 5430
只需终止进程……
杀死-9 5430
...您的数据库将被解锁。
其他回答
我刚刚遇到了类似的情况——我的web应用程序能够从数据库中读取数据,但不能执行任何插入或更新。重启Apache至少暂时解决了这个问题。
不过,如果能找到根本原因就好了。
我也有同样的问题。显然,回滚函数似乎用与db文件相同但没有最近更改的日志覆盖了db文件。我已经在下面的代码中实现了这一点,从那时起它一直工作得很好,而之前我的代码会因为数据库保持锁定而陷入循环。
希望这能有所帮助
我的python代码
##############
#### Defs ####
##############
def conn_exec( connection , cursor , cmd_str ):
done = False
try_count = 0.0
while not done:
try:
cursor.execute( cmd_str )
done = True
except sqlite.IntegrityError:
# Ignore this error because it means the item already exists in the database
done = True
except Exception, error:
if try_count%60.0 == 0.0: # print error every minute
print "\t" , "Error executing command" , cmd_str
print "Message:" , error
if try_count%120.0 == 0.0: # if waited for 2 miutes, roll back
print "Forcing Unlock"
connection.rollback()
time.sleep(0.05)
try_count += 0.05
def conn_comit( connection ):
done = False
try_count = 0.0
while not done:
try:
connection.commit()
done = True
except sqlite.IntegrityError:
# Ignore this error because it means the item already exists in the database
done = True
except Exception, error:
if try_count%60.0 == 0.0: # print error every minute
print "\t" , "Error executing command" , cmd_str
print "Message:" , error
if try_count%120.0 == 0.0: # if waited for 2 miutes, roll back
print "Forcing Unlock"
connection.rollback()
time.sleep(0.05)
try_count += 0.05
##################
#### Run Code ####
##################
connection = sqlite.connect( db_path )
cursor = connection.cursor()
# Create tables if database does not exist
conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS fix (path TEXT PRIMARY KEY);''')
conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS tx (path TEXT PRIMARY KEY);''')
conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS completed (fix DATE, tx DATE);''')
conn_comit( connection )
根据我的经验,此错误是由以下原因引起的:您打开了多个连接。
例如:
1个或多个sqlitebrowser (GUI) 一个或多个电子线 rails线程
我不确定SQLITE3如何处理多线程/请求的细节,但当我关闭sqlitebrowser和电子线程时,rails运行良好,不会再阻塞了。
我在使用Delphi和LiteDAC组件时得到了这个错误。 事实证明,它只发生在运行我的应用程序从Delphi IDE,如果连接属性设置为True的SQLite连接组件(在这种情况下TLiteConnection)。
我刚才在远程服务器上使用存储在NFS挂载上的SQLite数据库时遇到了这个问题。当我使用的远程shell会话在数据库打开时崩溃后,SQLite无法获得锁。
上面建议的恢复方法对我不起作用(包括先移动然后再复制数据库的想法)。但是在将其复制到非nfs系统后,数据库变得可用,数据似乎没有丢失。