当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当我输入这个查询: 删除邮件中id = 71的所有邮件
SQLite返回以下错误:
SQL error: database is locked
我如何解锁数据库,以便这个查询将工作?
当前回答
应该是数据库的内部问题… 对我来说,这是在尝试用“SQLite管理器”浏览数据库后表现出来的… 所以,如果你找不到另一个连接到数据库的进程,你也无法修复它, 试试这个激进的解决方案:
提供导出您的表(您可以在Firefox上使用“SQLite管理器”) 如果迁移改变了数据库方案,请删除上次失败的迁移 重命名“数据库”。sqlite”文件 执行“rake db:migrate”创建一个新的工作数据库 提供给正确的权限数据库表的导入 导入备份的表 编写新的迁移 执行"rake db:migrate"
其他回答
我在Mac OS X 10.5.7上从终端会话运行Python脚本时遇到了同样的问题。尽管我已经停止了脚本,并且终端窗口位于命令提示符处,但它在下次运行时仍然会给出这个错误。解决方案是关闭终端窗口,然后再次打开它。我觉得没道理,但奏效了。
如果一个进程在SQLite DB上有一个锁,并且崩溃了,那么这个DB将永远处于锁定状态。这就是问题所在。并不是其他进程有锁。
这个环节解决了问题。:当Sqlite给出:数据库锁定错误 它解决了我的问题也许对你有用。
并且可以使用开始事务和结束事务来避免将来数据库被锁定。
我也有同样的问题。显然,回滚函数似乎用与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 )
我的Linux环境上的lsof命令帮助我弄清楚一个进程挂起并保持文件打开。 终止了这个过程,问题就解决了。