Reputation:
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.
Upvotes: 3
Views: 6455
Reputation: 13695
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()
Upvotes: 4
Reputation: 650
I've also seen this error when the db file is on an NFS mounted file system.
Upvotes: 0
Reputation: 608
Deleting -journal files sounds like bad advice. See this explanation.
Upvotes: 2
Reputation: 73582
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.
Upvotes: 2