How to resolve: sqlite3.OperationalError: database is locked
The "database is locked" error in SQLite occurs when a database file is accessed by multiple processes or connections simultaneously, causing contention. Here’s how to resolve it:
🔍 Possible Causes & Fixes
1. Ensure Proper Connection Closing
If you're using Python, ensure that the database connection is closed properly after executing queries.
👉 Fix: Always call conn.close() after executing queries.
2. Check for Unclosed Transactions
If a transaction (like INSERT, UPDATE, or DELETE) is left open without COMMIT, SQLite keeps the database locked.
👉 Fix: Run COMMIT; in your SQL tool or application.
3. Close Other Processes Using the Database
Run the following SQL command in your SQLite session:
👉 Fix:
- If another process is using the DB file, close it (e.g., close DB Browser for SQLite, Python scripts, or any other app accessing the file).
- Restart your SQLite application.
4. Delete the .db-journal or .db-wal File
If SQLite crashed or was improperly closed, a journal file may still be locking the database.
- Navigate to your database folder
- Delete files ending in:
my_database.db-journalmy_database.db-wal
👉 Fix: Remove these files safely if no other process is using the database.
5. Open SQLite in Read-Write Mode
If another process is holding a write lock, try opening the DB in read-only mode:
👉 Fix: Use mode=ro if you don’t need to write to the database.
6. Kill Hanging Processes (Windows/Linux)
On Windows, find processes locking the file:
Kill a process using:
On Linux/macOS, find processes:
Kill the process:
👉 Fix: Restart any process holding the database.
🚀 Best Practices to Avoid "Database Locked" Issues
✅ Use PRAGMA busy_timeout:
✅ Use a Single Database Connection
Avoid opening multiple connections in the same script.
✅ Close the Connection Properly
Always call conn.close() after database operations.
✅ Use WAL Mode for Better Concurrency
Enable Write-Ahead Logging (WAL) for better handling of multiple connections:
Try these fixes, and let me know if the issue persists! 🚀
... ... ... ... ... ... ... ... ... ... ... ...
print the pid for "ps aux | grep sqlite"
To print only the PID of processes related to SQLite using ps aux | grep sqlite, use this command:
Explanation:
ps aux→ Lists all running processes.grep sqlite→ Filters processes containing "sqlite".awk '{print $2}'→ Extracts and prints only the PID (which is in the second column).
Alternative (More Precise)
If you want to exclude the grep command itself from the results:
Example Output:
These are the PIDs of processes running SQLite.
👉 To kill a process, use:
Let me know if you need more help! 🚀
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
ps -ef what does it do?
The ps -ef command in Linux is used to display a detailed list of all running processes on the system.
🔍 Breakdown of ps -ef
ps→ Shows active processes.-e→ Displays all processes (not just those owned by the current user).-f→ Provides full format output (more details).
📌 Example Output
📌 Column Explanation
| Column | Meaning |
|---|---|
| UID | User who started the process |
| PID | Process ID |
| PPID | Parent Process ID (which process started this one) |
| C | CPU usage percentage |
| STIME | Process start time |
| TTY | Terminal associated with the process (if any) |
| TIME | CPU time used |
| CMD | Command that started the process |
🔍 Useful Variations
✅ Show only processes of a specific user:
✅ Find a specific process (e.g., SQLite):
✅ Show hierarchical (parent-child) relationships:
Let me know if you need more details! 🚀😊