Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, sqlite error? db is locked? but it isnt?

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.


1 Answers

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()
like image 179
conradlee Avatar answered Jul 23 '26 15:07

conradlee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!