我有一个Python应用程序,它会抛出标准的sqlite3.OperationalError:数据库已锁定错误。我在互联网上搜索了一圈,没有找到任何解决方法(请注意,没有多进程/线程正在运行,并且正如您所见,我已尝试提高超时参数)。sqlite文件存储在本地硬盘上。
以下函数是访问sqlite数据库的众多函数之一,第一次调用时运行良好,但第二次调用时抛出上述错误(它作为另一个函数中的for循环的一部分被调用):
非常感谢。
以下函数是访问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
实例存储在函数作用域之外还是作为参数传递? - james the seventh__init__
方法中创建连接,并添加一个close_connection
(功能显而易见)方法,用于在使用完毕后关闭连接。否则,你可以搜索代码并在每次打开和关闭数据库连接时添加调试语句,以查看它们是否嵌套。 - tzot