SQLite Commands

This chapter will explain the simple yet useful commands used by SQLite programmers. These commands are called SQLite dot commands, and the difference is that they do not end with a semicolon;end.

Let's type a simplesqlite3command. At the SQLite command prompt, you can use various SQLite commands.

$ sqlite3
SQLite version 3.3.6
Enter ".help" for instructions
sqlite>

To get a list of the available dot commands, you can enter ".help" at any time. For example:

sqlite>.help

The above command will display a list of various important SQLite dot commands, as shown below:

CommandDescription
.backup ?DB? FILEBack up the DB database (default is "main") to the FILE file.
.bail ON|OFFStop after an error occurs. Default is OFF.
.databasesList the names of the databases and the files they are attached to.
.dump ?TABLE?Dump the database in SQL text format. If a TABLE table is specified, only dump TABLE tables that match the LIKE pattern.
.echo ON|OFFTurn echo on or off.
.exitExit the SQLite prompt.
.explain ON|OFFTurn on or off the output mode suitable for EXPLAIN. If no parameter is given, it is EXPLAIN on.
.header(s) ON|OFFTurn header display on or off.
.helpShow messages.
.import FILE TABLEImport data from the FILE file into the TABLE table.
.indices ?TABLE?Display the names of all indexes. If a TABLE table is specified, only display indexes of TABLE tables that match the LIKE pattern.
.load FILE ?ENTRY?Load an extension library.
.log FILE|offTurn logging on or off. FILE can be stderr (standard error)/stdout (standard output).
.mode MODESet the output mode, MODE can be one of the following:
  • csvcomma-separated values

  • columnleft-aligned columns

  • htmlHTML <table> code

  • insertSQL INSERT statements for TABLE tables

  • lineone value per line

  • listvalues separated by .separator strings

  • tabsvalues separated by Tab

  • tclTCL list elements

.nullvalue STRINGOutput STRING string in place of NULL values.
.output FILENAMESend output to the FILENAME file.
.output stdoutSend output to the screen.
.print STRING...Output STRING string verbatim.
.prompt MAIN CONTINUEReplace the standard prompt.
.quitExit the SQLite prompt.
.read FILENAMEExecute SQL in the FILENAME file.
.schema ?TABLE?Display CREATE statements. If a TABLE table is specified, only display TABLE tables that match the LIKE pattern.
.separator STRINGChange the output mode and the separator used by .import.
.showDisplay the current values of various settings.
.stats ON|OFFTurn statistics on or off.
.tables ?PATTERN?List the names of tables matching the LIKE pattern.
.timeout MSTry opening locked tables for MS milliseconds.
.width NUM NUMSet column widths for "column" mode.
.timer ON|OFF Turn the CPU timer on or off.

Let's try using.showcommand to view the default settings of the SQLite command prompt.

sqlite>.show
     echo: off
  explain: off
  headers: off
     mode: column
nullvalue: ""
   output: stdout
separator: "|"
    width:
sqlite>
Make sure there is no space between the sqlite> prompt and the dot command, otherwise it will not work properly.

Formatting Output

You can use the following dot commands to format the output into the formats listed below in this tutorial:

sqlite>.header on
sqlite>.mode column
sqlite>.timer on
sqlite>

The above settings will produce output in the following format:

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0
CPU Time: user 0.000000 sys 0.000000

sqlite_master Table

The master table stores the key information of database tables and names itsqlite_master. To view the table summary, do the following:

sqlite>.schema sqlite_master

This will produce the following result:

CREATE TABLE sqlite_master (
  type text,
  name text,
  tbl_name text,
  rootpage integer,
  sql text
);
Other Extensions