Launch Offer: Use codelaunch30for 30% off

SQLITE_NOTADB: file is not a database

This error occurs when SQLite tries to open a file that isn't a valid SQLite database. The file might be corrupted, encrypted, or simply not a database file.

The file is not a database error means the file doesn't have a valid SQLite format.

Understanding the Error

SQLITE_NOTADB: file is not a database

SQLite checks the file header and found it doesn't match the expected SQLite format.

Common Causes

1. File is Empty

A newly created but empty file:

JAVASCRIPT
// This creates an empty file
fs.writeFileSync('mydb.db', '');

// SQLite can't open it
const db = new Database('mydb.db');  // Error

2. File is Encrypted

SQLite Encryption Extension (SEE) or SQLCipher encrypted database:

JAVASCRIPT
// Encrypted database opened without key
const db = new Database('encrypted.db');  // Fails

3. Wrong File Type

Opening a non-database file:

JAVASCRIPT
// Accidentally opening a log file
const db = new Database('./app.log');  // Not a database!

4. File Corrupted

The header was damaged:

BASH
# Check file header
hexdump -C mydb.db | head -1
# Should start with: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74
# Which is: "SQLite format"

5. Incomplete Download/Copy

File transfer was interrupted.

How to Fix It

Solution 1: Create Fresh Database

JAVASCRIPT
const fs = require('fs');

// Delete invalid file
if (fs.existsSync('mydb.db')) {
  fs.unlinkSync('mydb.db');
}

// SQLite will create a new valid database
const db = new Database('mydb.db');
db.exec('CREATE TABLE test (id INTEGER)');

Solution 2: Provide Encryption Key

For encrypted databases:

JAVASCRIPT
// SQLCipher
const db = new Database('encrypted.db');
db.pragma(`key = 'your-secret-key'`);

Solution 3: Verify File is SQLite

BASH
# Check file type
file mydb.db
# Should output: SQLite 3.x database

# Or check header
head -c 16 mydb.db
# Should show: SQLite format 3

Solution 4: Restore from Backup

If corrupted, restore a known good backup.

Solution 5: Check File Path

Make sure you're opening the right file:

JAVASCRIPT
const path = require('path');

const dbPath = path.resolve('./data/mydb.db');
console.log('Opening:', dbPath);
console.log('Exists:', fs.existsSync(dbPath));
console.log('Size:', fs.statSync(dbPath).size);

Debugging

BASH
# Check if it's a SQLite file
sqlite3 mydb.db ".schema"

# Check file header bytes
xxd -l 16 mydb.db

# Valid SQLite header:
# 00000000: 5351 4c69 7465 2066 6f72 6d61 7420 3300  SQLite format 3.

Best Practices

  1. Don't manually create database files - let SQLite do it
  2. Verify downloads with checksums
  3. Back up databases regularly
  4. Use proper file extensions to avoid confusion