3

I get "database table is locked" error in my sqlite3 db. My script is single threaded, no other app is using the program (i did have it open once in "SQLite Database Browser.exe"). I copied the file, del the original (success) and renamed the copy so i know no process is locking it yet when i run my script everything in table B cannot be written to and it looks like table A is fine. Whats happening?

-edit- I fixed it but unsure how. I notice the code not doing the correct things (i copied the wrong field) and after fixing it up and cleaning it, it magically started working again.

-edit2-

Someone else posted so i might as well update. I think the problem was i was trying to do a statement with a command/cursor in use.

2
  • can you reduce your code to a minimal non-working example and post it?
    – nosklo
    Feb 10, 2009 at 10:18
  • Without code, it's impossible to tell what you're talking about. BTW, if your app is multihreaded or creates subprocesses, include that fact, also.
    – S.Lott
    Feb 10, 2009 at 11:09

4 Answers 4

4

I have run into this problem before also. It occurs often when you have a cursor and connection open and then your program crashes before you can close it properly. In some cases the following function can be used to make sure that the database is unlocked, even after it was not properly committed and closed beforehand:

from sqlite3 import dbapi2 as sqlite


def unlock_db(db_filename):
    """Replace db_filename with the name of the SQLite database."""
    connection = sqlite.connect(db_filename)
    connection.commit()
    connection.close()
2

Maybe your application terminated prematurely after a SQLite transaction began. Look for stale -journal files in the directory and delete them.

It might be worth skimming through the documentation as well.

1
2

Deleting -journal files sounds like bad advice. See this explanation.

0

I've also seen this error when the db file is on an NFS mounted file system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.