[英]Python sqlite3 “unable to open database file” on windows
我正在使用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 7(python 2.6,django 1.1.1和sqllite)上的錯誤如下:sqlite3.OperationalError:無法打開數據庫文件
我從Eclipse運行我的腳本不同時間,總是遇到錯誤。 但是當我從命令行運行它(在設置PYTHONPATH和DJANGO_SETTINGS_MODULE之后)它就像魅力一樣......
只需2美分!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.