An sqlite3 Primer

Once you have some form of SQLite installed, the first step is normally to run sqlite3 and play around. The sqlite3 tool accepts SQL commands from an interactive prompt and passes those commands to the SQLite core for processing.

Even if you have no intention of distributing a copy of sqlite3 with your application, it is extremely useful to have a copy around for testing and debugging queries. If your application uses a customized build of the SQLite core, you will likely want to build a copy of sqlite3 using the same build parameters.

To get started, just run the SQLite command. If you provide a filename (such as test.db), sqlite3 will open (or create) that file. If no filename is given, sqlite3 will automatically open an unnamed temporary database:

$ sqlite3 test.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

The sqlite> prompt means sqlite3 is ready to accept commands. We can start with some basic expressions:

sqlite> SELECT 3 * 5, 10;
15|10
sqlite>

SQL commands can also be entered across multiple lines. If no terminating semicolon is found, the statement is assumed to continue. In that case, the prompt will change to ...> to indicate sqlite3 is waiting for more input:

sqlite> SELECT 1 + 2,
    ...> 6 + 3;
3|9
sqlite>

If you ever find yourself at the ...> prompt unexpectedly, make sure you finished up the previous line with a semicolon.

In addition to processing SQL statements, there is a series of shell-specific ...

Get Using SQLite now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.