Discover how to use and probe a SQLite database
Pragmas and Quotation Marks
A PRAGMA
statement is a SQL extension that changes the general high-level behavior of the SQLite library or checks the status and properties of a data structure (Figure 2). One example of pragma is

PRAGMA auto_vacuum = FULL;
which tells SQLite to enable automatic execution of the VACUUM
command, which then rebuilds and compacts database files to save disk space.
SQLite uses single quotes to enclose literal strings and double quotes for keywords or column or table names. Assuming your database has a column named platform, you may write:
platform='linux' platform="linux"
But the first statement means "[do something] if the field called platform has the value linux"; the second statement says "[do something] if the field called platform has the same value as the field called linux."
Metacommands
SQLite metacommands, called dot commands because they begin with a dot, don't add to or fetch data from SQLite tables. They provide high-level information, shortcuts to frequently used metaqueries, or change the way query results are presented, as shown in Table 1.
Table 1
Some SQLite Metacommands
Dot Command | Action |
---|---|
|
List all active databases |
|
List all metacommands |
|
Import data from |
|
Send output to |
|
Send output to the screen |
|
Execute the SQL commands in |
|
List the complete structure of the current databases |
|
Only show structure of the requested table |
|
Change column separator |
|
Show current settings |
|
List all the database tables |
The same table also shows how to add comments to your SQLite code: you can use two dashes (--
), which means the parser ignores everything that follows to the end of the line. For multiline comments, you enclose text between the strings /*
and */
, as you would in C programs.
Data Types
In a SQLite table, each column can store data of different types, but each column has its own preferred storage class (Figure 3). This class is assigned to each column when you create a table, such as:
CREATE TABLE books (bookid INTEGER PRIMARY KEY, title TEXT NOT NULL, author TEXT NOT NULL, price REAL NOT NULL);

The books table has four columns: the first, which is also the index, is an integer; the last column is a floating-point number, and the others are text strings.
Dates deserve an extra word because they can be stored in three interchangeable formats:
REAL
– the number of days since noon, Greenwich mean time, on November 24, 4714BCINTEGER
– the number of seconds since 1970-01-01 00:00:00 UTCTEXT
– a string (e.g.,"YYYY-MM-DD HH:MM:SS.SSS"
)
Speaking of text strings, beware! Although the SQLite core library can store strings with any encoding, by default it correctly compares and orders only ASCII characters in a case-insensitive way. This design choice makes the SQLite core code as small and fast as possible. Support for non-ASCII characters is delegated to external libraries, which in practice may be linked already into the SQLite software packaged for your Linux distribution. SQLite can also store BLOBs, which are raw sequences of bytes (e.g., images).
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.
-
openSUSE Leap 15.5 Beta Now Available
The final version of the Leap 15 series of openSUSE is available for beta testing and offers only new software versions.