Académique Documents
Professionnel Documents
Culture Documents
In this part of the SQLite tutorial, we will cover the sqlite3 command line tool.
The manual describes the sqlite3 as follows: sqlite3 is a terminal-based front-end to the SQLite library
that can evaluate queries interactively and display the results in multiple for‐ mats. sqlite3 can also be
used within shell scripts and other applica‐ tions to provide batch processing features.
sqlite3 tool
sqlite3 is a terminal based frontend to the SQLite library that can evaluate queries interactively and
display the results in multiple formats. It can also be used within scripts.
On the terminal screen, we see the following prompt of the sqlite3 tool.
$ sqlite3 test.db
sqlite>
The .help is one of the meta commnads of the sqlite3 tool. It shows a list of them. The .exit and the
The .quit commands exit the sqlite3 session. The .databases command shows the attached
Creating a database
The complete SQLite database is stored in a single cross-platform disk file. We use the sqlite3
command line tool to create a new database file.
$ sqlite3 movies.db
sqlite> .tables
1|Tom
2|Lucy
3|Frank
4|Jane
5|Robert
Here we get the output of a simple SELECT statement. By default, the output mode is line and the
separator is |.
sqlite> .separator :
1:Tom
2:Lucy
3:Frank
4:Jane
5:Robert
There are several other output modes available. The following example will show the column output
mode.
sqlite> .headers on
Id Name
---------- ----------
1 Tom
2 Lucy
3 Frank
4 Jane
5 Robert
In this example, we have our data in the column mode. Plus we show the column headers with the
Title Author
------------- -----------
Here, the column widths are not wide enough to display all data correctly.
Title Author
---------------------- ------------------
Here we change the column widths. The first column will be 22 characters wide, the second 18.
sqlite> .show
echo: off
explain: off
headers: off
mode: list
nullvalue: ""
output: stdout
separator: "|"
width:
The .show command lists various settings. We can see the output mode, the separator used in the list
CREATE TABLE Cars(Id integer primary key, Name text, Cost integer);
The .schema command shows the structure of the table. It gives the DDL SQL to create the table.
Id Name Cost
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
Here we have non interactively executed a SELECT SQL command. We selected all cars from the Cars
table.
Dumping tables
It is possible to dump tables in SQL format to the disk. This way we can easily save the structure and
Id Name Cost
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
Now, we are going to use the .dump command to dump the table.
BEGIN TRANSACTION;
CREATE TABLE Cars(Id integer primary key, Name text, Cost integer);
COMMIT;
The .dump command shows us the SQL necessary to recreate the table.
We can also redirect the output to a file. The .output command will redirect the output to the cars.sql
file.
$ cat cars.sql
BEGIN TRANSACTION;
CREATE TABLE Cars(Id integer primary key, Name text, Cost integer);
...
We verify it.
Reading SQL
We can read SQL from a file name with the .read commnad.
Cars
Cars
Id Name Cost
1 Audi 52642
Here we have executed a series of commands. We drop the table and read it from the cars.sql, that we
Resource file
The sqlite3 tool has a resource file called .sqliterc. It is located in the home directory. If there is no
such file, we can simply create it. We can place the meta commnads there or the regular SQL
$ cat .sqliterc
.mode column
.header on
.nullvalue NULL
Here is a simple example of a resource file. It has three meta commands. With resource file, we don't
have to execute meta commnads all over again, when we start the sqlite3 tool. They will be executed
We have a message saying, that the tool loaded resources upon the beginning.
The tool has several command line options. They mostly duplicate the meta commands. Note, that
$ sqlite3 -version
3.5.9
<TR><TH>Id</TH><TH>Name</TH><TH>Cost</TH></TR>
<TR><TD>1</TD>
<TD>Audi</TD>
<TD>52642</TD>
</TR>
<TR><TD>2</TD>
<TD>Mercedes</TD>
<TD>57127</TD>
</TR>
The -html option causes the results to be output as simple HTML tables
In this part of the SQLite tutorial, we worked with the sqlite3 command line tool. We have described
various meta commnads. We have shown, how to dump tables, read SQL from files; we described the