file is not a database
SQLITE_NOTADB (result code 26) is returned when SQLite opens a file that does not begin with the expected SQLite database header magic bytes ("SQLite format 3\000"). This happens when the application accidentally opens a non-database file (a CSV, a JSON file, a PDF) using sqlite3.connect(), or when an encrypted database is opened without the decryption key.
- 1The file path points to a non-SQLite file (text file, binary, etc.)
- 2The database file is encrypted (e.g., SQLCipher) and is being opened without the key
- 3The file is a SQLite database but uses a page size or header format from an incompatible fork
- 4The file was partially written and the header is incomplete
sqlite3.connect() is called on a plain text file.
import sqlite3
# Create a text file, not a database
with open('/tmp/notadb.txt', 'w') as f:
f.write('this is not a database\n')
conn = sqlite3.connect('/tmp/notadb.txt')
conn.execute('SELECT * FROM t') # triggers SQLITE_NOTADBexpected output
sqlite3.DatabaseError: file is not a database
Fix
Verify the file is a valid SQLite database before connecting
WHEN When the file path may be wrong or the file type is uncertain.
import sqlite3
def is_sqlite_db(path):
try:
with open(path, 'rb') as f:
header = f.read(16)
return header == b'SQLite format 3\x00'
except OSError:
return False
if is_sqlite_db('/tmp/mydb.db'):
conn = sqlite3.connect('/tmp/mydb.db')
else:
raise ValueError("Not a valid SQLite database file")Why this works
Every valid SQLite database file starts with the 16-byte magic string "SQLite format 3\000". Checking this header before connecting gives a clear error message rather than the generic SQLITE_NOTADB error.
✕ Connect to an encrypted SQLite database without providing the key
Encrypted SQLite files (SQLCipher, SEE) will appear as SQLITE_NOTADB because the header is encrypted. Attempting to read them as plain SQLite will not work.
SQLITE_NOTADB result code is stable. The magic header check is part of the database file format specification.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev