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

  1. Open or create a database file: sqlite3 practice.db. If the file does not exist, it is created when you first write to it.
  2. List tables: .tables. Quick check that you opened the right file.
  3. See how a table was created: .schema pets. Shows columns, types and constraints at a glance.
  4. 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

  1. Turn on column headers: .headers on. You see which value is which.
  2. Use a table-like layout: .mode column or, in newer versions, .mode box or .mode table. Rows line up neatly.
  3. 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

  1. Run a file of SQL from inside the shell: .read schema.sql. Rebuild your tables in one step.
  2. Or from your terminal: sqlite3 practice.db < seed.sql. Handy in setup scripts.

Getting data in and out

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

  1. Import a CSV into a table: .import --csv data.csv songs in recent versions. Check .help import for the options your version supports.
  1. Dump everything as SQL: .dump > backup.sql from the terminal (sqlite3 practice.db .dump > backup.sql). A readable backup.

Understanding queries

  1. Time a query: .timer on. Shows how long each statement took.
  2. See the query plan: EXPLAIN QUERY PLAN SELECT ...;. Shows whether SQLite scans the table or uses an index.
  3. Turn on foreign keys for this session: PRAGMA foreign_keys = ON;. SQLite does not enforce them otherwise.

Leaving

  1. Quit: .quit or .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

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