Skip to content

NOWWORKINGON

I create temporary SQLite database files on almost a daily basis

Some relevant links:
- https://sqlite-utils.datasette.io is the Python CLI tool and library I wrote to speed up this kind of work.
Here’s a recent tutorial I wrote about it: https://datasette.io/tutorials/clean-data

Python SQLite: database is locked

check:
1. you don’t have a hung process sitting on the file :
see if fuser {dbname}  returns a line , it should be nothing , if a PID is returned , check that PID with ps {PID}
2. There isn’t a {dbname}-journal file in the directory with cache.db; this would indicate a crashed session that hasn’t been cleaned up properly.
3. Ask the database shell to check itself: $ sqlite3 cache.db "pragma integrity_check;"
4. Backup the database $ sqlite3 cache.db ".backup cache.db.bak"
5. Remove cache.db as you probably have nothing in it (if you are just learning) and try your code again
6. See if the backup works $ sqlite3 cache.db.bak ".schema"

Failing that, read Things That Can Go Wrong and How to Corrupt Your Database Files