Windows 上的 Python sqlite3 "unable to open database file"

标签 python sqlite windows-vista

我正在 python 3.1.1 中的 windows vista 机器上工作。我正在尝试将大量行插入到 SQLite3 数据库中。该文件存在,我的程序正确地将 一些 行插入到数据库中。但是,在插入过程的某个时刻,程序会终止并显示以下消息: sqlite3.OperationalError: 无法打开数据库文件

但是,在它消亡之前,有几行已正确添加到数据库中。

这是专门处理插入的代码:

idx = 0
lst_to_ins = []
for addl_img in all_jpegs:
    lst_to_ins.append((addl_img['col1'], addl_img['col2']))
    idx = idx + 1
    if idx % 10 == 0:
        logging.debug('adding rows [%s]', lst_to_ins)
        conn.executemany(ins_sql, lst_to_ins)
        conn.commit()
        lst_to_ins = []
        logging.debug('added 10 rows [%d]', idx)
if len(lst_to_ins) > 0:
    conn.executemany(ins_sql, lst_to_ins)
    conn.commit()
    logging.debug('adding the last few rows to the db')

此代码插入 10 到 400 行的任意位置,然后终止并显示错误消息

conn.executemany(ins_sql, lst_to_ins)
sqlite3.OperationalError: unable to open database file

我怎么可能插入一些行,但随后出现此错误?

最佳答案

SQLite 没有记录锁定;它使用一种简单的锁定机制,在写入过程中短暂锁定整个数据库文件。听起来您遇到了尚未清除的锁。

SQLite 的作者建议您在执行插入之前创建一个事务,然后在最后完成该事务。这会导致 SQLite 将插入请求排队,并在提交事务时使用单个文件锁执行它们。

在最新版本的 SQLite 中,锁定机制得到了增强,因此可能不再需要完整的文件锁定。

关于Windows 上的 Python sqlite3 "unable to open database file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1529527/

相关文章:

python - 当 urllib2.urlopen 抛出异常时捕获响应正文

android - Sqlite 数据库 Pragma 的异常?

windows - 哪些操作需要在 Windows 中提升 UAC?

python - 使用 Matplotlib 创建概率/频率轴网格(不规则间隔)

Python加速重建列表的代码

macos - 在cli的单个命令中将多个命令提交给sqlite3

android - 从文件插入 SQLite 数据库,只插入一行

delphi - 如何在 Vista 上安装 Delphi 7

windows - 在 Vista 和 XP 中模拟 Control-Alt-Delete 键序列

来自 StringIO 源的 Python xml etree DTD?