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
![](/var/linux_magazin/storage/images/issues/2016/186/sqlite-tutorial/figure-2/669117-1-eng-US/Figure-2_large.png)
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);
![](/var/linux_magazin/storage/images/issues/2016/186/sqlite-tutorial/figure-3/669120-1-eng-US/Figure-3_large.png)
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
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.