Opening a Database
Opening a database in SQLite usually means opening a file path.
If the file exists, SQLite uses it. If it does not, SQLite can create it (when writable).
Core Concepts
flowchart TD
A[sqlite3 path/to/file.db] --> B{File exists?}
B -->|Yes| C[Open existing database]
B -->|No| D[Create new database file]
C --> E[sqlite> prompt]
D --> E
| Action | Command |
|---|---|
| Open existing file | sqlite3 app.db |
| Open in-memory DB | sqlite3 ":memory:" |
| Show attached DBs | .databases |
- From Terminal
- Inside CLI
# Open a file-backed database from your terminal.
sqlite3 app.db
| Expected output |
|---|
SQLite prompt appears (sqlite>). |
-- If already in sqlite3, switch/open with .open.
.open app.db
.databases
| Expected output |
|---|
.databases shows main mapped to app.db. |
Code Examples
-- Confirm which database file is active.
.databases
| Expected output |
|---|
A row like main: /full/or/relative/path/app.db |
SQLite-Specific Nuances
SQLite Nuance
The active database is usually named main in SQLite.
Additional files can be attached later under other names.
Common Pitfalls / Best Practices
Pitfall
Running sqlite3 app.db from the wrong directory and accidentally creating a new empty file with the same name.
Best Practice
Use explicit paths (or run .databases) whenever you are unsure which file is open.
Quick Challenge
Open practice.db, then run .databases and identify the path for main.
View Solution
sqlite3 practice.db
| Expected result |
|---|
Prompt opens; .databases shows main pointing to practice.db. |