unable to open database file
SQLITE_CANTOPEN (result code 14) is returned when SQLite cannot open the specified database file. This occurs at connection time, before any SQL is executed. Common causes include the directory not existing, insufficient permissions, or a too-long path. In WAL mode, failure to create the -wal or -shm sidecar files also produces this error.
- 1The directory containing the database file does not exist
- 2The process lacks read permission on the database file
- 3The process lacks write permission on the containing directory (needed to create the journal or WAL sidecar files)
- 4The path exceeds the OS maximum path length
- 5The temporary directory (for in-memory or :memory: URIs) is unavailable
Connecting to a database in a directory that does not exist.
import sqlite3
# The directory /nonexistent/path/ does not exist
conn = sqlite3.connect('/nonexistent/path/demo.db') # triggers SQLITE_CANTOPENexpected output
sqlite3.OperationalError: unable to open database file
Fix
Create the parent directory before connecting
WHEN When the database path is in a directory that may not yet exist.
import sqlite3, os db_path = '/var/data/myapp/demo.db' os.makedirs(os.path.dirname(db_path), exist_ok=True) conn = sqlite3.connect(db_path)
Why this works
os.makedirs with exist_ok=True creates all intermediate directories atomically. SQLite then finds the parent directory and can create or open the database file.
✕ Silently fall back to :memory: when the file cannot be opened
In-memory databases are lost when the connection closes. If the application expects persistent storage and silently falls back, all data written during the session will be lost.
SQLITE_CANTOPEN_NOTEMPDIR (270) extended code introduced — emitted when a temporary file needed for sorting or WAL cannot be created because TMPDIR is unavailable or full.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev