当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当前回答
如果你想删除一个“database is locked”错误,请按照以下步骤执行:
将数据库文件复制到其他位置。 用复制的数据库替换数据库。这将解除对访问数据库文件的所有进程的引用。
其他回答
SQLite wiki DatabaseIsLocked页面提供了此错误消息的解释。在某种程度上,它指出争用的来源是内部的(对发出错误的进程而言)。本页没有解释的是SQLite如何决定进程中的某些东西持有锁,以及哪些条件会导致误报。
当您试图从同一个数据库连接同时对数据库执行两项不兼容的操作时,就会出现此错误代码。
在v3中引入的有关文件锁定的变化可能对未来的读者有用,可以在这里找到:SQLite版本3中的文件锁定和并发性
根据我的经验,此错误是由以下原因引起的:您打开了多个连接。
例如:
1个或多个sqlitebrowser (GUI) 一个或多个电子线 rails线程
我不确定SQLITE3如何处理多线程/请求的细节,但当我关闭sqlitebrowser和电子线程时,rails运行良好,不会再阻塞了。
从您之前的评论中,您说存在一个-journal文件。
这可能意味着您已经打开和(EXCLUSIVE?)事务,还没有提交数据。是你的程序或者其他进程留下了-journal吗?
重新启动sqlite进程将查看日志文件,清除任何未提交的操作并删除-journal文件。
我添加了“池=true”连接字符串,它工作。
我也有同样的问题。显然,回滚函数似乎用与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 )