Practical Tips for the SQLite Command-Line Shell
The sqlite3 shell is a small program with a lot of handy features hidden behind dot-commands. These tips make it far more pleasant for practising SQL. Each one has a short reason.
Opening and exploring
- Open or create a database file:
sqlite3 practice.db. If the file does not exist, it is created when you first write to it. - List tables:
.tables. Quick check that you opened the right file. - See how a table was created:
.schema pets. Shows columns, types and constraints at a glance. - Get help:
.help. Lists every dot-command available in your version.
Dot-commands start with a full stop and do not need a semicolon. SQL statements do.
Making output readable
- Turn on column headers:
.headers on. You see which value is which. - Use a table-like layout:
.mode columnor, in newer versions,.mode boxor.mode table. Rows line up neatly. - Show NULLs clearly:
.nullvalue NULL. Otherwise empty and NULL look the same.
Put commands you always want into a file called .sqliterc in your home directory; the shell runs it at start-up.
Running scripts
- Run a file of SQL from inside the shell:
.read schema.sql. Rebuild your tables in one step. - Or from your terminal:
sqlite3 practice.db < seed.sql. Handy in setup scripts.
Getting data in and out
- Export a query to CSV:
.headers on
.mode csv
.output songs.csv
SELECT * FROM songs;
.output stdout
The last line sends output back to the screen.
- Import a CSV into a table:
.import --csv data.csv songsin recent versions. Check.help importfor the options your version supports.
- Dump everything as SQL:
.dump > backup.sqlfrom the terminal (sqlite3 practice.db .dump > backup.sql). A readable backup.
Understanding queries
- Time a query:
.timer on. Shows how long each statement took. - See the query plan:
EXPLAIN QUERY PLAN SELECT ...;. Shows whether SQLite scans the table or uses an index. - Turn on foreign keys for this session:
PRAGMA foreign_keys = ON;. SQLite does not enforce them otherwise.
Leaving
- Quit:
.quitor.exit, or press Ctrl+D on most systems.
Making the shell your own
Once you know a few commands, put your favourites in .sqliterc so every session starts the way you like: headers on, a readable mode and a visible NULL marker. Keep a small text file of queries you run often, and paste them in or load them with .read. Many experienced developers still reach for this little shell first, because it opens instantly and shows exactly what the database did.
Cheat sheet
| Task | Command |
|---|---|
| List tables | .tables |
| Show table definition | .schema name |
| Readable output | .headers on and .mode column |
| Run a script | .read file.sql |
| Export CSV | .mode csv and .output file.csv |
| Time queries | .timer on |
| Quit | .quit |
Where next
Our library shelf of free tools describes graphical alternatives, and your first slow query shows what to look for in EXPLAIN QUERY PLAN output.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.