sqlite3.OperationalError:数据库被锁定 - 非线程应用程序

时间:2010-04-02 21:14:40

标签: python sql sqlite

我有一个Python应用程序会抛出标准的sqlite3.OperationalError: database is locked错误。我查看了互联网,找不到任何有效的解决方案(请注意,没有多进程/线程正在进行,你可以看到我已经尝试提高超时参数)。 sqlite文件存储在本地硬盘上。

以下函数是访问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()

非常感谢。

2 个答案:

答案 0 :(得分:2)

您可能会特别检查保持读锁定的功能(未完成的光标)。这将阻止更新功能的提交。请注意,有一个专门的Python-sqlite问题邮件列表:http://groups.google.com/group/python-sqlite

答案 1 :(得分:1)

您是否真的需要为每个UPDATE连续打开和关闭数据库文件?如果在访问数据库的每个函数中执行相同的操作,是否可能已从另一个已使用不同连接打开数据库的函数调用update_index函数并且修改数据库的过程?