English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQLite Commands

This chapter will guide you through the simple and useful commands used by SQLite programmers. These commands are called SQLite point commands, but the exception is that they should not be terminated with a semicolon (;).

Let's start withsqlite3Start by entering a simple command at the command prompt, which will provide you with a SQLite command prompt where you can issue various SQLite commands.

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

For a list of available point commands, you can enter '.help' at any time. For example-

sqlite>.help

The above commands will display a list of various important SQLite point commands. The table below lists these commands.

NumberCommand and description
1

.backup ?DB? FILE

Backup database (default is 'main') to FILE

2

.bail ON|OFF

Stop after encountering an error. Default is off

3

.databases

List the names and files of attached databases

4

.dump ?TABLE?

Dump the database in SQL text format. If TABLE is specified, only dump tables that match the LIKE pattern TABLE

5

.echo ON|OFF

Open or close command echoing

6

.exit

Exit the SQLite prompt

7

.explain ON|OFF

Open or close output mode suitable for EXPLAIN. Without parameters, it will open EXPLAIN

8

.header(s) ON|OFF

Open or close header display

9

.help

Display this message

10

.import FILE TABLE

Import data from FILE to TABLE

11

.indices ?TABLE?

Display the names of all indices. If TABLE is specified, only display indices of tables that match the LIKE pattern TABLE

12

.load FILE ?ENTRY?

Load extension library

13

.log FILE|off

Open or close login. FILE can be stderr / stdout

14

.mode MODE

Set MODE to one of the following output modes-

  • csv − values separated by comma

  • column − left-aligned columns.

  • html − HTML <table> code

  • insert − SQL insert statement for TABLE

  • line − one value per line

  • list − values separated by .separator string

  • tabs -tab-separated values

  • tcl − TCL list element

15

.nullvalue STRING

Print STRING instead of NULL value

16

.output FILENAME

Send output to FILENAME

17

.output stdout

Send output to the screen

18Age

.print STRING...

Print the text STRING

19

.prompt MAIN CONTINUE

Replace the standard prompt

20

.quit

Exit the SQLite prompt

21

.read FILENAME

Execute SQL in FILENAME

22

.schema ?TABLE?

Display CREATE statements. If TABLE is specified, only tables matching the LIKE pattern TABLE are displayed

23

.separator STRING

Change the output mode and the delimiter used by .import

24

.show

Display the current values of various settings

25

.stats ON|OFF

Open or close statistics

26

.tables ?PATTERN?

List the names of tables that match the LIKE pattern

27

.timeout MS

Try to open a locked table, in milliseconds

28

.width NUM NUM

Set the column width in 'column' mode

29

.timer ON|OFF

Open or close CPU timer measurement

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 are no spaces between the sqlite> prompt and the dot commands, otherwise it will not work properly.

Formatted output

You can use the following dot command sequence to format the output.

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

The above settings will produce the following format of output.

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 main table saves key information about the database tables, which is calledsqlite_masterYou can see its schema as follows:

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);