Command Line Shell For SQLite
Command Line Shell For SQLite
1. Getting Started
$ sqlite3 ex1
SQLite version 3.36.0 2021-06-18 18:36:39
Enter ".help" for usage hints.
sqlite> create table tbl1(one text, two int);
sqlite> insert into tbl1 values('hello!',10);
sqlite> insert into tbl1 values('goodbye', 20);
sqlite> select * from tbl1;
hello!|10
goodbye|20
sqlite>
Make sure you type a semicolon at the end of each SQL command!
The sqlite3 program looks for a semicolon to know when your SQL
command is complete. If you omit the semicolon, sqlite3 will give
you a continuation prompt and wait for you to enter more text to
complete the SQL command. This feature allows you to enter SQL
commands that span multiple lines. For example:
sqlite> CREATE TABLE tbl2 (
...> f1 varchar(30) primary key,
...> f2 text,
...> f3 real
...> );
sqlite>
Most of the time, sqlite3 just reads lines of input and passes them on
to the SQLite library for execution. But input lines that begin with a
dot (".") are intercepted and interpreted by the sqlite3 program itself.
These "dot commands" are typically used to change the output
format of queries, or to execute certain prepackaged query
statements. There were originally just a few dot commands, but over
the years many new features have accumulated so that today there
are over 60.
For a listing of the available dot commands, you can enter ".help"
with no arguments. Or enter ".help TOPIC" for detailed information
about TOPIC. The list of available dot-commands follows:
sqlite> .help
.archive ... Manage SQL archives
.auth ON|OFF Show authorizer
callbacks
.backup ?DB? FILE Backup DB (default
"main") to FILE
.bail on|off Stop after hitting an
error. Default OFF
.binary on|off Turn binary output on or
off. Default OFF
.cd DIRECTORY Change the working
directory to DIRECTORY
.changes on|off Show number of rows
changed by SQL
.check GLOB Fail if output since
.testcase does not match
.clone NEWDB Clone data into NEWDB
from the existing database
.connection [close] [#] Open or close an
auxiliary database connection
.databases List names and files of
attached databases
.dbconfig ?op? ?val? List or change
sqlite3_db_config() options
.dbinfo ?DB? Show status information
about the database
.dump ?OBJECTS? Render database content
as SQL
.echo on|off Turn command echo on or
off
.eqp on|off|full|... Enable or disable
automatic EXPLAIN QUERY PLAN
.excel Display the output of
next command in spreadsheet
.exit ?CODE? Exit this program with
return-code CODE
.expert EXPERIMENTAL. Suggest
indexes for queries
.explain ?on|off|auto? Change the EXPLAIN
formatting mode. Default: auto
.filectrl CMD ... Run various
sqlite3_file_control() operations
.fullschema ?--indent? Show schema and the
content of sqlite_stat tables
.headers on|off Turn display of headers
on or off
.help ?-all? ?PATTERN? Show help text for
PATTERN
.import FILE TABLE Import data from FILE
into TABLE
.imposter INDEX TABLE Create imposter table
TABLE on index INDEX
.indexes ?TABLE? Show names of indexes
.limit ?LIMIT? ?VAL? Display or change the
value of an SQLITE_LIMIT
.lint OPTIONS Report potential schema
issues.
.load FILE ?ENTRY? Load an extension
library
.log FILE|off Turn logging on or off.
FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode
.nonce STRING Disable safe mode for
one command if the nonce matches
.nullvalue STRING Use STRING in place of
NULL values
.once ?OPTIONS? ?FILE? Output for the next SQL
command only to FILE
.open ?OPTIONS? ?FILE? Close existing database
and reopen FILE
.output ?FILE? Send output to FILE or
stdout if FILE is omitted
.parameter CMD ... Manage SQL parameter
bindings
.print STRING... Print literal STRING
.progress N Invoke progress handler
after every N opcodes
.prompt MAIN CONTINUE Replace the standard
prompts
.quit Exit this program
.read FILE Read input from FILE
.recover Recover as much data as
possible from corrupt db.
.restore ?DB? FILE Restore content of DB
(default "main") from FILE
.save FILE Write in-memory database
into FILE
.scanstats on|off Turn
sqlite3_stmt_scanstatus() metrics on or off
.schema ?PATTERN? Show the CREATE
statements matching PATTERN
.selftest ?OPTIONS? Run tests defined in the
SELFTEST table
.separator COL ?ROW? Change the column and
row separators
.session ?NAME? CMD ... Create or control
sessions
.sha3sum ... Compute a SHA3 hash of
database content
.shell CMD ARGS... Run CMD ARGS... in a
system shell
.show Show the current values
for various settings
.stats ?ARG? Show stats or turn stats
on or off
.system CMD ARGS... Run CMD ARGS... in a
system shell
.tables ?TABLE? List names of tables
matching LIKE pattern TABLE
.testcase NAME Begin redirecting output
to 'testcase-out.txt'
.testctrl CMD ... Run various
sqlite3_test_control() operations
.timeout MS Try opening locked
tables for MS milliseconds
.timer on|off Turn SQL timer on or off
.trace ?OPTIONS? Output each SQL
statement as it is run
.vfsinfo ?AUX? Information about the
top-level VFS
.vfslist List all available VFSes
.vfsname ?AUX? Print the name of the
VFS stack
.width NUM1 NUM2 ... Set minimum column
widths for columnar output
sqlite>
SQL statements;
dot-commands; or
CLI comments
It must begin with its "." at the left margin with no preceding
whitespace.
It must be entirely contained on a single input line.
It cannot occur in the middle of an ordinary SQL statement. In
other words, it cannot occur at a continuation prompt.
There is no comment syntax for dot-commands.
The CLI also accepts whole-line comments that begin with a '#'
character and extend to the end of the line. There can be no with
whitespace prior to the '#'.
ascii
box
csv
column
html
insert
json
line
list
markdown
quote
table
tabs
tcl
You can use the ".mode" dot command to switch between these
output formats. The default output mode is "list". In list mode, each
row of a query result is written on one line of output and each
column within that row is separated by a specific separator string.
The default separator is a pipe symbol ("|"). List mode is especially
useful when you are going to send the output of a query to another
program (such as AWK) for additional processing.
sqlite> .mode
current output mode: list
sqlite>
one = goodbye
two = 20
sqlite>
sqlite> .width
sqlite> .mode markdown
sqlite> select * from tbl1;
| one | two |
|---------|-----|
| hello! | 10 |
| goodbye | 20 |
sqlite> .mode table
sqlite> select * from tbl1;
+---------+-----+
| one | two |
+---------+-----+
| hello! | 10 |
| goodbye | 20 |
+---------+-----+
sqlite> .mode box
sqlite> select * from tbl1;
┌─────────┬─────┐
│ one │ two │
├─────────┼─────┤
│ hello! │ 10 │
│ goodbye │ 20 │
└─────────┴─────┘
sqlite>
Other output modes include "csv", "json", and "tcl". Try these yourself
to see what they do.
sqlite> .tables
tbl1 tbl2
sqlite>
As with ".tables", the ".schema" command shows the schema for all
attached databases. If you only want to see the schema for a single
database (perhaps "main") then you can add an argument to
".schema" to restrict its output:
sqlite> .schema main.*
sqlite> .databases
If the --new option is included with ".open", then the database is reset
prior to being opened. Any prior data is destroyed. This is a
destructive overwrite of prior data and no confirmation is requested,
so use this option carefully.
8. Redirecting I/O
sqlite> .once -x
sqlite> SELECT * FROM bigTable;
The ".excel" command is an alias for ".once -x". It does exactly the
same thing.
If the argument to ".read" begins with the "|" character, then instead
of opening the argument as a file, it runs the argument (without the
leading "|") as a command, then uses the output of that command as
its input. Thus, if you have a script that generates SQL, you can
execute that SQL directly using a command similar to the following:
The readfile(X) SQL function reads the entire content of the file
named X and returns that content as a BLOB. This can be used to
load content into a table. For example:
The writefile(X,Y) SQL function write the blob Y into the file named X
and returns the number of bytes written. Use this function to extract
the content of a single table column into a file. For example:
The CLI has another built-in SQL function named edit(). Edit() takes
one or two arguments. The first argument is a value - often a large
multi-line string to be edited. The second argument is the invocation
for a text editor. (It may include options to affect the editor's
behavior.) If the second argument is omitted, the VISUAL
environment variable is used. The edit() function writes its first
argument into a temporary file, invokes the editor on the temporary
file, rereads the file back into memory after the editor is done, then
returns the edited text.
The edit() function can be used to make changes to large text values.
For example:
In this example, the content of the docs.body field for the entry
where docs.name is "report-15" will be sent to the editor. After the
editor returns, the result will be written back into the docs.body field.
The default operation of edit() is to invoke a text editor. But by using
an alternative edit program in the second argument, you can also get
it to edit images or other non-text resources. For example, if you
want to modify a JPEG image that happens to be stored in a field of
a table, you could run:
The edit program can also be used as a viewer, by simply ignoring the
return value. For example, to merely look at the image above, you
might run:
To import into a table not in the "main" schema, the --schema option
may be used to specify that the table is in some other schema. This
can be useful for ATTACH'ed databases or to import into a TEMP
table.
When .import is run, its treatment of the first input row depends upon
whether the target table already exists. If it does not exist, the table
is automatically created and the content of the first input row is used
to set the name of all the columns in the table. In this case, the table
data content is taken from the second and subsequent input rows. If
the target table already exists, every row of the input, including the
first, is taken to be actual data content. If the input file contains an
initial row of column labels, you can make the .import command skip
that initial row using the "--skip 1" option.
To export an SQLite table (or part of a table) as CSV, simply set the
"mode" to "csv" and then run a query to extract the desired rows of
the table.
sqlite> .headers on
sqlite> .mode csv
sqlite> .once c:/work/dataout.csv
sqlite> SELECT * FROM tab1;
sqlite> .system c:/work/dataout.csv
In the example above, the ".headers on" line causes column labels to
be printed as the first row of output. This means that the first row of
the resulting CSV file will contain column labels. If column labels are
not desired, set ".headers off" instead. (The ".headers off" setting is
the default and can be omitted if the headers have not been
previously turned on.)
The line ".once FILENAME" causes all query output to go into the
named file instead of being printed on the console. In the example
above, that line causes the CSV content to be written into a file
named "C:/work/dataout.csv".
On Linux and other unix systems you will need to enter something
like:
The command above writes the output of the query as CSV into a
temporary file, invokes the default handler for CSV files (usually the
preferred spreadsheet program such as Excel or LibreOffice), then
deletes the temporary file. This is essentially a short-hand method of
doing the sequence of ".csv", ".once", and ".system" commands
described above.
The ".excel" command is really an alias for ".once -x". The -x option to
.once causes it to writes results as CSV into a temporary file that is
named with a ".csv" suffix, then invoke the systems default handler
for CSV files.
The command-line shell uses the Zipfile virtual table to access ZIP
archives. You can see this by running the ".schema" command when
a ZIP archive is open:
sqlite> .schema
CREATE VIRTUAL TABLE zip USING
zipfile('document.docx')
/* zip(name,mode,mtime,sz,rawdata,data,method)
*/;
When opening a file, if the command-line client discovers that the file
is ZIP archive instead of an SQLite database, it actually opens an in-
memory database and then in that in-memory database it creates an
instance of the Zipfile virtual table that is attached to the ZIP archive.
The text format is pure SQL so you can also use the .dump command
to export an SQLite database into other popular SQL database
engines. Like this:
$ createdb ex2
$ sqlite3 ex1 .dump | psql ex2
The "lost_and_found" table contains one row for each orphaned row
recovered from the database. Additionally, there is one row for each
recovered index entry that cannot be attributed to any SQL index.
This is because, in an SQLite database, the same format is used to
store SQL index entries and WITHOUT ROWID table entries.
Column Contents
SQLite computes the entry point for the extension based on the
extension filename. To override this choice, simply add the name of
the extension as a second argument to the ".load" command.
The ".selftest --init" command creates the selftest table if it does not
already exists, then appends entries that check the SHA3 hash of the
content of all tables. Subsequent runs of ".selftest" will verify that the
database has not been changed in any way. To generate tests to
verify that a subset of the tables is unchanged, simply run ".selftest --
init" then DELETE the selftest rows that refer to tables that are not
constant.
15. SQLite Archive Support
Long and short style options may be mixed. For example, the
following are equivalent:
Extract files from the archive (either to the current working directory
or to the directory specified by a --directory option). Files or
directories whose names match the arguments, as affected by the --
glob option, are extracted. Or, if no arguments follow the options, all
files and directories are extracted. Any specified directories are
extracted recursively. It is an error if any specified names or match
patterns cannot be found in the archive.
For the --insert command, all files listed are inserted into the archive.
For the --update command, files are only inserted if they do not
previously exist in the archive, or if their "mtime" or "mode" is
different from what is currently in the archive.
And if there is an entry in that table where the key column exactly
matches the name of parameter (including the initial "?", "$", ":", or
"@" character) then the parameter is assigned the value of the value
column. If no entry exists, the parameter defaults to NULL.
The ".parameter" command exists to simplify managing this table.
The ".parameter init" command (often abbreviated as just ".param
init") creates the temp.sqlite_parameters table if it does not already
exist. The ".param list" command shows all entries in the
temp.sqlite_parameters table. The ".param clear" command drops
the temp.sqlite_parameters table. The ".param set KEY VALUE" and
".param unset KEY" commands create or delete entries from the
temp.sqlite_parameters table.
The VALUE passed to ".param set KEY VALUE" can be either a SQL
literal or any other SQL expression or query which can be evaluated
to yield a value. This allows values of differing types to be set. If such
evaluation fails, the provided VALUE is instead quoted and inserted
as text. Because such initial evaluation may or may not fail
depending upon the VALUE content, the reliable way to get a text
value is to enclose it with single-quotes protected from the above-
described command-tail parsing. For example, (unless one intends a
value of -1365):
.parameter init
.parameter set @phoneNumber "'202-456-1111'"
In the above, the user creates the database schema (a single table -
"x1"), and then uses the ".expert" command to analyze a query, in this
case "SELECT * FROM x1 WHERE a=? AND b>?". The shell tool
recommends that the user create a new index (index
"x1_idx_000123a7") and outputs the plan that the query would use in
EXPLAIN QUERY PLAN format. The user then creates an index with
an equivalent schema and runs the analysis on the same query
again. This time the shell tool does not recommend any new indexes,
and outputs the plan that SQLite will use for the query given the
existing indexes.
Option Purpose
The CLI is built with several SQLite extensions that are not included
with the SQLite library. A few add features not described in the
preceding sections, namely:
There are many command-line options available to the CLI. Use the --
help command-line option to see a list:
$ sqlite3 --help
Usage: ./sqlite3 [OPTIONS] FILENAME [SQL]
FILENAME is the name of an SQLite database. A new
database is created
if the file does not previously exist.
OPTIONS include:
-A ARGS... run ".archive ARGS" and
exit
-append append the database to
the end of the file
-ascii set output mode to
'ascii'
-bail stop after hitting an
error
-batch force batch I/O
-box set output mode to 'box'
-column set output mode to
'column'
-cmd COMMAND run "COMMAND" before
reading stdin
-csv set output mode to 'csv'
-deserialize open the database using
sqlite3_deserialize()
-echo print commands before
execution
-init FILENAME read/process named file
-[no]header turn headers on or off
-help show this message
-html set output mode to HTML
-interactive force interactive I/O
-json set output mode to 'json'
-line set output mode to 'line'
-list set output mode to 'list'
-lookaside SIZE N use N entries of SZ bytes
for lookaside memory
-markdown set output mode to
'markdown'
-maxsize N maximum size for a --
deserialize database
-memtrace trace all memory
allocations and deallocations
-mmap N default mmap size set to
N
-newline SEP set output row separator.
Default: '\n'
-nofollow refuse to open symbolic
links to database files
-nonce STRING set the safe-mode escape
nonce
-nullvalue TEXT set text string for NULL
values. Default ''
-pagecache SIZE N use N slots of SZ bytes
each for page cache memory
-quote set output mode to
'quote'
-readonly open the database read-
only
-safe enable safe-mode
-separator SEP set output column
separator. Default: '|'
-stats print memory stats before
each finalize
-table set output mode to
'table'
-tabs set output mode to 'tabs'
-version show SQLite version
-vfs NAME use NAME as the default
VFS
-zip open the file as a ZIP
Archive
Suppose you want to run a suspicious script and the script requires
one or two of the features that --safe normally disables. For example,
suppose it needs to ATTACH one additional database. Or suppose
the script needs to load a specific extension. This can be
accomplished by preceding the (carefully audited) ATTACH
statement or the ".load" command with an appropriate ".nonce"
command and supplying the same nonce value using the "--nonce"
command-line option. Those specific commands will then be
allowed to execute normally, but all other unsafe commands will still
be restricted.
sh configure; make
nmake /f Makefile.msc