Discover how to use and probe a SQLite database
Operators and Functions
In all variants of SQL, operators are tools that let you compare data among records or process them in several ways (e.g., to make calculations or reformat text strings). SQLite supports all the basic arithmetic (e.g., equality, the four operators (+ - * /), inequalities, etc.) and logical operators (e.g., AND
and OR
), just to name the most common. Some operators are combinations of simpler ones, like BETWEEN
. For example, although the two statements
SELECT * FROM budget WHERE (cost BETWEEN 10 AND 20); SELECT * FROM budget WHERE ((cost >= 10) AND (cost <= 20);
both mean the same thing – that is, "show me all data of all the expenses in the table called budget whose cost is between 10 and 20 dollars" – the first version is faster both to write and execute.
The last SQLite operator to introduce is LIKE
, which is used to compare strings. In the statement
SELECT * FROM sales WHERE customer LIKE '%William_';
the final underscore means that any single character in this position is valid, whereas the percent sign means that any sequence of characters in this position is valid. Therefore, the statement will display the sales made to customers with names like John Williams, B. Williamx, Mike T. Williamm, and so on.
Functions are operators that directly "extract" one number or string from (combinations of) other numbers or strings, for example:
abs(X)
returns the absolute value of the number X.round(X,Y)
returns the number X rounded to Y decimal digits.substr(X,Y,Z)
returns Z consecutive characters of string X, starting from position Y.substr('Linux', 3,2)
returns the string 'ux'.
SQLite has many more core and aggregate functions that are available by default, as well as functions you can use by loading extensions.
Adding, Updating, and Selecting Records
The whole point of a database is to store data in an ordered way, keep it current, and above all search and filter the data with any combination of criteria. This work is done with the SQL commands INSERT
, UPDATE
, DELETE
, and SELECT
. In the next examples, assume you keep a catalog of all your books in the books table defined earlier. Listing 1 shows how you would add, update, and delete a book from the SQLite command prompt. (See the "Using SQLite at the Command Line" box.)
Listing 1
Simple SQLIte Statements
sqlite> INSERT INTO books(title, author, price) VALUES('Lord of the rings', 'Tolkien' 25.50); sqlite> UPDATE books SET price=20.50 WHERE bookid=21; sqlite> DELETE FROM books WHERE ((title='Lord of the rings') AND (price=20.50));
Using SQLite at the Command Line
To get a SQLite command prompt, enter sqlite3 <dbn>
, where <dbn>
is the name of the file with the database you want to access. If you don't specify a name, SQLite creates a temporary database; if the name you enter doesn't exist, SQLite creates it. Ctrl+D stops the program, and Ctrl+C stops a SQL statement.
The first command adds a book to the database, and the second command corrects its price (assuming the automatically generated ID of that book record is 21). The third command removes all the records in the table with the same title and updated price.
In the next statement, the SELECT
command, which has already been introduced, finds all the records that match certain conditions in the specified table and displays them grouped or sorted according to other criteria:
sqlite> SELECT title as TITLE, sum(price) AS 'total_expense' \ FROM books WHERE author='Tolkien' GROUP BY book_title \ ORDER BY total_expense DESC;
In this case, it produces a list of how much you have spent on each Tolkien book in your library:
- in total (courtesy of the
sum()
function) - even if you have more than one edition of the same book (thanks to the
GROUP BY
clause) - from the most to least expensive (because of the
ORDER BY
clause in descending,DESC
, order)
You can SELECT
from multiple tables simultaneously by combining them with JOIN
statements, as I will demonstrate in a moment.
Practical Examples
The first cool thing you might want to do is get all or part of your data out of the database, formatted in ways that you can then easily process and reuse with other software.
For example, you can save to a text file all the SQL statements that would create a perfect copy of your database by using the .output
and .dump
metacommands:
sqlite> .output mybackup.sql sqlite> .dump sqlite> .exit
Even better, you can save the same commands in a text file (or have other scripts generate them for you) and then run them with the sqlite3
tool from the Linux command line:
# sqlite3 < commands.sqlite
To restore or clone a SQLite database from the dump, type (or, again, put it into a shell script) this command, instead:
# sqlite3 new.db < mybackup.sql
The file passed to sqlite3
can contain any sequence of commands, including SELECT
statements. Also, the data written to the file set by .output
can be formatted in any of the ways supported by another very useful dot command called .mode
: You can export your data as HTML tables, comma- or tab-separated files that are immediately usable in any spreadsheet, and five or six other formats.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.