SQLite CLI Shell for SQLite

Version 3.46.1 · source

Start here

sqlite3 <file.db>
Open or create a database
.tables
List tables
.schema <table>
Show CREATE statements
.mode box
Readable table output
.headers on
Show column names
.import --csv <file> <table>
Load a CSV
.backup <file>
Binary copy of the database
.quit
Leave the shell

Concepts

  1. Input file, SQL arg, stdin
  2. Shell dot-commands + SQL
  3. Formatter box, csv, json...
  4. Output stdout, file or pipe

Open and run

sqlite3 <file.db>
Open (or create) a database
sqlite3 <file.db> "<sql>"
Run one statement and exit
sqlite3 <file.db> < <script.sql>
Run a SQL script
.read <script.sql>
Run a script from inside the shell
.open <file.db>
Switch to another database file
--new wipes an existing file without asking
sqlite3 -readonly <file.db>
Open without write access
.help
List all dot-commands
.quit
Exit the shell
.shell <cmd>
Run a shell command
.cd <dir>
Change working directory
sqlite3 -bail -batch <file.db>
Stop at the first error
sqlite3 -safe <file.db>
Disable file access and shell escapes
Not a full sandbox
sqlite3 -init <file>
Run a startup script first
.timeout <ms>
Wait for a locked database

Output modes

.mode box
Unicode box table
.mode column
Aligned plain columns
.mode table
ASCII table
.mode markdown
Markdown table
.mode csv
Comma separated values
.mode json
JSON array of objects
.mode line
One column per line
.mode list
Default pipe-separated
.mode tabs
Tab separated values
Does not re-import cleanly if values hold double quotes
.mode insert <table>
Rows as INSERT statements
.mode quote
SQL-quoted values
.headers on
Print column names
.nullvalue <text>
Show NULL as text
.separator <col> <row>
Set delimiters
.width <n> <n>
Fixed column widths
.show
Show current settings

Inspect schema

.tables
List tables and views
.tables <pattern>
Filter with a LIKE pattern
.schema
All CREATE statements
.schema <table>
CREATE statements for one table
.schema --indent
Pretty-printed schema
.indexes <table>
List indexes
.databases
Attached databases and files
.dbinfo
Page size, counts, schema cookie
.fullschema
Schema plus sqlite_stat data
Statistics may expose sampled data values
.lint fkey-indexes
Foreign keys lacking an index
.expert
Suggest indexes for next query
Experimental
pragma table_info(<table>);
Columns and types

Import and export

.import --csv <file> <table>
Load CSV into a table
A missing table is created from the header row
.import --csv --skip 1 <file> <table>
Load into an existing table
Without --skip the header becomes a data row
.once <file>
Send next result to a file
.output <file>
Send all results to a file
.output
Back to stdout
.once | <cmd>
Pipe next result to a command
.once -x
Open next result in a spreadsheet
select readfile('<path>');
Read a file as a value
select writefile('<path>', <blob>);
Write a value to a file
.print <text>
Print a literal line

Backup and recover

.backup <file>
Binary copy of main database
Overwrites an existing file silently
.restore <file>
Load a backup into this database
.save <file>
Write database to a file
.dump
Whole database as SQL text
.dump <table>
One table as SQL text
sqlite3 <old.db> .dump | sqlite3 <new.db>
Rebuild into a fresh file
.recover
Salvage data from a corrupt file
Orphaned rows land in lost_and_found
.clone <newdb>
Copy content into a new file
.sha3sum
Hash of database content
pragma integrity_check;
Check for corruption

Debug and tune

.timer on
Time each statement
.eqp on
Show query plan automatically
explain query plan <sql>;
Plan for one statement
.stats on
Memory and I/O statistics
.changes on
Report rows changed
.echo on
Echo commands as they run
.trace stdout
Print each executed statement
.load <extension>
Load an extension library
.parameter set <name> <value>
Bind a named parameter
.version
Show library version info

Recipes

CSV into a new database
sqlite3 app.db# inside the shell:.import --csv people.csv people.schema peopleselect count(*) from people;
Export a query to CSV
sqlite3 -header -csv app.db "select * from people" > people.csv# or inside the shell:.headers on.mode csv.once people.csvselect * from people;
Safe live backup
sqlite3 app.db ".backup 'snap.db'"sqlite3 snap.db "pragma integrity_check;"
Quick look at an unknown database
.tables.mode box.headers on.schema --indentselect * from <table> limit 5;