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:
| Command | Description |
|---|---|
| .backup ?DB? FILE | Back up the DB database (default is "main") to the FILE file. |
| .bail ON|OFF | Stop after an error occurs. Default is OFF. |
| .databases | List 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|OFF | Turn echo on or off. |
| .exit | Exit the SQLite prompt. |
| .explain ON|OFF | Turn on or off the output mode suitable for EXPLAIN. If no parameter is given, it is EXPLAIN on. |
| .header(s) ON|OFF | Turn header display on or off. |
| .help | Show messages. |
| .import FILE TABLE | Import 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|off | Turn logging on or off. FILE can be stderr (standard error)/stdout (standard output). |
| .mode MODE | Set the output mode, MODE can be one of the following:
|
| .nullvalue STRING | Output STRING string in place of NULL values. |
| .output FILENAME | Send output to the FILENAME file. |
| .output stdout | Send output to the screen. |
| .print STRING... | Output STRING string verbatim. |
| .prompt MAIN CONTINUE | Replace the standard prompt. |
| .quit | Exit the SQLite prompt. |
| .read FILENAME | Execute 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 STRING | Change the output mode and the separator used by .import. |
| .show | Display the current values of various settings. |
| .stats ON|OFF | Turn statistics on or off. |
| .tables ?PATTERN? | List the names of tables matching the LIKE pattern. |
| .timeout MS | Try opening locked tables for MS milliseconds. |
| .width NUM NUM | Set 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