Skip to main content

Creating a New Database

A new SQLite database is created the moment SQLite needs to write to a new file path.

That means database creation is lightweight: no server setup, no separate admin console.

Core Concepts

flowchart LR
A[Choose filename] --> B[sqlite3 new.db]
B --> C[Run first write SQL]
C --> D[(new.db exists on disk)]
StepWhy it matters
Pick a stable filenameEasier scripts and backups
Open with sqlite3 new.dbStarts working session
Execute first writeEnsures file is actually initialized

Code Examples

# Open (or create) a new database file.
sqlite3 school.db
Expected output
SQLite prompt appears.
-- In the sqlite3 prompt, run a simple statement.
-- This confirms SQL execution in the new database session.
SELECT 'new database session' AS status;
status
new database session
-- Show which file is attached as main.
.databases
Expected output
main entry points to school.db.

SQLite-Specific Nuances

SQLite Nuance

SQLite databases are ordinary files. Creation happens lazily when SQLite opens a path and performs operations requiring storage.

Common Pitfalls / Best Practices

Pitfall

Assuming opening a filename always means you opened an existing populated database.

If the file did not exist, you may now be in a fresh empty database.

Best Practice

Immediately run .databases after opening a file so you know exactly where you are working.

Quick Challenge

Create notes.db and run a query that returns created as result.

View Solution
SELECT 'created' AS result;
result
created