Dave Viner
Dave Viner

Reputation: 539

Python sqlite3 "unable to open database file" on windows

I am working on a windows vista machine in python 3.1.1. I am trying to insert a large number of rows into a SQLite3 db. The file exists, and my program properly inserts some rows into the db. However, at some point in the insertion process, the program dies with this message: sqlite3.OperationalError: unable to open database file

However, before it dies, there are several rows that are properly added to the database.

Here is the code which specifically handles the insertion:

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')

This code inserts anywhere from 10 to 400 rows, then dies with the error message

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

How is it possible that I can insert some rows, but then get this error?

Upvotes: 7

Views: 4614

Answers (2)

Sergio Morstabilini
Sergio Morstabilini

Reputation: 2055

same error here on windows 7 (python 2.6, django 1.1.1 and sqllite) after some records inserted correctly: sqlite3.OperationalError: unable to open database file

I ran my script from Eclipse different times and always got that error. But as I ran it from the command line (after setting PYTHONPATH and DJANGO_SETTINGS_MODULE) it worked as a charm...

just my 2 cents!

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180787

SQLite does not have record locking; it uses a simple locking mechanism that locks the entire database file briefly during a write. It sounds like you are running into a lock that hasn't cleared yet.

The author of SQLite recommends that you create a transaction prior to doing your inserts, and then complete the transaction at the end. This causes SQLite to queue the insert requests, and perform them using a single file lock when the transaction is committed.

In the newest version of SQLite, the locking mechanism has been enhanced, so it might not require a full file lock anymore.

Upvotes: 1

Related Questions