pgref.dev/sqlite/errors/SQLITE_NOTADB
SQLITE_NOTADBERRORTier 1 — Safe✅ HIGH confidence

file is not a database

Category: File AccessVersions: All SQLite versions

What this means

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.

Why it happens

  1. 1The file path points to a non-SQLite file (text file, binary, etc.)
  2. 2The database file is encrypted (e.g., SQLCipher) and is being opened without the key
  3. 3The file is a SQLite database but uses a page size or header format from an incompatible fork
  4. 4The file was partially written and the header is incomplete

How to reproduce

sqlite3.connect() is called on a plain text file.

trigger — this will ERROR
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_NOTADB
sqlite3.DatabaseError: file is not a database

Fix 1: Verify the file is a valid SQLite database before connecting

When the file path may be wrong or the file type is uncertain.

fix
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.

What not to do

Connect to an encrypted SQLite database without providing the key

Why it's wrong: 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.

Version notes

All versionsSQLITE_NOTADB result code is stable. The magic header check is part of the database file format specification.

Sources

📚 Official docs: https://www.sqlite.org/rescode.html#notadb

🔧 Source ref: sqlite3.h — SQLITE_NOTADB = 26

📖 Further reading: SQLite database file format

Confidence assessment

✅ HIGH confidence

Stable. The magic header bytes are specified in the file format documentation and have not changed.

See also

📄 Reference pages

SQLite file formatSQLCipher
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →