我有一个Python应用程序抛出标准sqlite3.OperationalError: database is locked
错误。我查看了互联网,找不到任何可行的解决方案(请注意,没有多进程/线程正在进行,正如您所看到的,我已尝试提高超时参数)。 sqlite文件存储在本地硬盘上。sqlite3.OperationalError:数据库被锁定 - 非线程应用程序
以下函数是访问sqlite数据库的许多函数之一,它在第一次调用时运行良好,但在第二次调用时会抛出上述错误(在另一个函数中调用for
循环的一部分功能):
def update_index(filepath):
path = get_setting('Local', 'web')
stat = os.stat(filepath)
modified = stat.st_mtime
index_file = get_setting('Local', 'index')
connection = sqlite3.connect(index_file, 30)
cursor = connection.cursor()
head, tail = os.path.split(filepath)
cursor.execute('UPDATE hwlive SET date=? WHERE path=? AND name=?;', (modified, head, tail))
connection.commit()
connection.close()
非常感谢。
数据库在调用此函数之前应该完全关闭。如果我打算保持数据库连接,那么最好的方法是什么?将'connection'实例存储在函数的作用域之外,还是将其作为参数传递? – 2010-04-04 13:14:58
这将是整洁,如果你犯了一个类,把处理中的类数据库中的所有功能,有连接作为类的成员属性。你可以在'__init__'类中创建连接,并添加一个close_connection(具有明显的内容)方法,当你完成时调用。否则,你可以搜索你的代码,并添加调试语句,只要你打开和关闭与数据库的连接,查看它们是否嵌套。 – tzot 2010-04-04 15:44:03