Windows上的Python sqlite3"无法打开数据库文件"

Dav*_*ner 6 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')
Run Code Online (Sandbox Code Playgroud)

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

conn.executemany(ins_sql, lst_to_ins)
sqlite3.OperationalError: unable to open database file
Run Code Online (Sandbox Code Playgroud)

我怎么可能插入一些行,但后来得到这个错误?

Rob*_*vey 1

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

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

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