SQLite Wikibook
SQLite Wikibook
SQLite
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/SQLite
Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons
Attribution-ShareAlike 3.0 License.
Introduction
Design
Unlike client–server database management systems, the SQLite engine has no standalone processes with which the application program communicates. Instead,
the SQLite library is linked in and thus becomes an integral part of the application program. The library can also be called dynamically. The application
program uses SQLite's functionality through simple function calls, which reduce latency in database access: function calls within a single process are more
efficient than inter-process communication. SQLite stores the entire database (definitions, tables, indices, and the data itself) as a single cross-platform file on a
host machine. It implements this simple design by locking the entire database file during writing. SQLite read operations can be multitasked, though writes can
only be performed sequentially.
Due to the server-less design, SQLite applications require less configuration than client-server databases. SQLite is called zero-conf[1] because it does not
require service management (such as startup scripts) or access control based on GRANT and passwords. Access control is handled by means of file system
permissions given to the database file itself. Databases in client-server systems use file system permissions which give access to the database files only to the
daemon process.
Another implication of the serverless design is that several processes may not be able to write to the database file. In server-based databases, several writers will
all connect to the same daemon, which is able to handle its locks internally. SQLite on the other hand has to rely on file-system locks. It has less knowledge of
the other processes that are accessing the database at the same time. Therefore, SQLite is not the preferred choice for write-intensive deployments.[2] However,
for simple queries with little concurrency, SQLite performance profits from avoiding the overhead of passing its data to another process.
SQLite uses PostgreSQL as a reference platform. “What would PostgreSQL do” is used to make sense of the SQL standard.[3][4] One major deviation is that,
with the exception of primary keys, SQLite does not enforce type checking; the type of a value is dynamic and not strictly constrained by the schema (although
the schema will trigger a conversion when storing, if such a conversion is potentially reversible). SQLite strives to follow Postel's Rule.[5]
History
D. Richard Hipp designed SQLite in the spring of 2000 while working for General Dynamics on contract with the United States Navy.[6] Hipp was designing
software used aboard guided missile destroyers, which originally used HP-UX with an IBM Informix database back-end. SQLite began as a Tcl extension.[7]
The design goals of SQLite were to allow the program to be operated without installing a database management system or requiring a database administrator.
Hipp based the syntax and semantics on those of PostgreSQL 6.5. In August 2000, version 1.0 of SQLite was released, with storage based on gdbm (GNU
Database Manager). SQLite 2.0 replaced gdbm with a custom B-tree implementation, adding transaction capability. SQLite 3.0, partially funded by America
Online, added internationalization, manifest typing, and other major improvements.
In 2011 Hipp announced his plans to add a NoSQL interface (managing documents expressed in JSON) to SQLite databases and to develop UnQLite, an
embeddable document-oriented database.[8]. UnQLite was released as an independent database.[9]
Features
SQLite implements most of the SQL-92 standard for SQL but it lacks some features. For example, it partially provides triggers, and it cannot write to views
(however it provides INSTEAD OF triggers that provide this functionality). While it provides complex queries, it still has limited ALTER TABLE function, as it
cannot modify or delete columns.[10]
SQLite uses an unusual type system for an SQL-compatible DBMS; instead of assigning a type to a column as in most SQL database systems, types are
assigned to individual values; in language terms it is dynamically typed. Moreover, it is weakly typed in some of the same ways that Perl is: one can insert a
string into an integer column (although SQLite will try to convert the string to an integer first, if the column's preferred type is integer). This adds flexibility to
columns, especially when bound to a dynamically typed scripting language. However, the technique is not portable to other SQL products. A common criticism
is that SQLite's type system lacks the data integrity mechanism provided by statically typed columns in other products. The SQLite web site describes a "strict
affinity" mode, but this feature has not yet been added.[5] However, it can be implemented with constraints like CHECK(typeof(x)='integer').[6]
Several computer processes or threads may access the same database concurrently. Several read accesses can be satisfied in parallel. A write access can only be
satisfied if no other accesses are currently being serviced. Otherwise, the write access fails with an error code (or can automatically be retried until a
configurable timeout expires). This concurrent access situation would change when dealing with temporary tables. This restriction is relaxed in version 3.7
when write-ahead logging (WAL) is turned on enabling concurrent reads and writes.[12]
SQLite version 3.7.4 first saw the addition of the FTS4 (full text search) module, which features enhancements over the older FTS3 module.[13] FTS4 allows
users to perform full text searches on documents similar to how search engines search webpages.[14] Version 3.8.2 added support for creating tables without
rowid,[15] which may provide space and performance improvements.[16] Common table expressions support was added to SQLite in version 3.8.3.[17]
In 2015, with the json1 extension[18] and new subtype interfaces, SQLite version 3.9 introduced JSON content managing.
A standalone command-line program is provided in SQLite's distribution. It can be used to create a database, define tables, insert and change rows, run queries
and manage an SQLite database file. It also serves as an example for writing applications that use the SQLite library.
SQLite uses automated regression testing prior to each release. Over 2 million tests are run as part of a release's verification. Starting with the August 10, 2009
release of SQLite 3.6.17, SQLite releases have 100% branch test coverage, one of the components of code coverage. The tests and test harnesses are partially
public domain and partially proprietary.[20]
Notable users
The browsers Google Chrome, Opera, Safari and the Android Browser all allow for storing information in, and retrieving it from, a SQLite
database within the browser, using the Web SQL Database technology, although this is rapidly becoming deprecated (namely superseded by
IndexedDB).
Mozilla Firefox and Mozilla Thunderbird store a variety of configuration data (bookmarks, cookies, contacts etc.) in internally managed SQLite
databases. There is a third-party add-on that uses the code supporting this functionality to provide a user interface for managing arbitrary
SQLite databases.[21]
Various web application frameworks
Adobe Systems uses SQLite as its file format in Adobe Photoshop Lightroom, a standard database in Adobe AIR, and internally within Adobe
Reader.[7]
Evernote uses SQLite to store its local database repository in Windows.
Skype[22]
Apple[23] adopted it as an option in macOS's Core Data API from the original implementation in Mac OS X 10.4 onwards, and also for
administration of videos and songs, and in iOS for storage of text messages on the iPhone.
Windows 10[24]
References
Citations
1. "SQLite Is A Zero-Configuration Database". SQLite.org. Retrieved 8. "Interview: Richard Hipp on UnQL, a New Query Language for
August 3, 2015. Document Databases". InfoQ. August 4, 2011. Retrieved October 5,
2. "Appropriate Uses For SQLite". SQLite.org. Retrieved 2015-09-03. 2011.
3. "PGCon 2014: Clustering and VODKA". Lwn.net. Retrieved 2017- 9. UnQLite FAQ (https://unqlite.org/faq.html#10)
01-06. 10. "SQL Features That SQLite Does Not Implement". SQLite.org.
4. "PGCon2014: SQLite: Protégé of PostgreSQL". Pgcon.org. January 1, 2009. Retrieved October 14, 2009.
Retrieved 2017-01-06. 11. "Case-insensitive matching of Unicode characters does not work".
5. "SQLite: StrictMode". Sqlite.org. Retrieved September 3, 2015. SQLite Frequently Asked Questions. Retrieved 2015-09-03.
6. Owens, Michael (2006). The Definitive Guide to SQLite. Apress. 12. "Write Ahead Logging in SQLite 3.7". SQLite.org. Retrieved
doi:10.1007/978-1-4302-0172-4_1. ISBN 978-1-59059-673-9. September 3, 2011. "WAL provides more concurrency as readers do
7. "Well-Known Users Of SQLite". SQLite. Retrieved August 5, 2015. not block writers and a writer does not block readers. Reading and
writing can proceed concurrently"
13. "SQLite Release 3.7.4 On 2010-12-08". SQLite.org. December 8,
2010. Retrieved September 3, 2015.
14. "SQLite FTS3 and FTS4 Extensions". SQLite.org. Retrieved 24. "SQLite databases". Msdn.microsoft.com. 2016-09-19. Retrieved
September 3, 2015. 2017-01-06.
15. "SQLite Release 3.8.2 On 2013-12-06". SQLite.org. December 6, 25. "User Defined Functions". Autoitscript.com. Retrieved 2017-01-06.
2013. Retrieved September 3, 2015. 26. "SQLite3 API for GNU Emacs 25+". github.com. Retrieved 2017-10-
16. "The WITHOUT ROWID Optimization". SQLite.org. Retrieved 02.
September 3, 2015. 27. Ross McKinlay; Colin Bull. "SQLProvider". Fsprojects.github.io.
17. "SQLite Release 3.8.3 On 2014-02-03". SQLite.org. February 3, Retrieved 2017-01-06.
2014. Retrieved September 3, 2015. 28. "Google Code Archive - Long-term storage for Google Code Project
18. https://sqlite.org/json1.html Hosting". Code.google.com. Retrieved 2017-01-06.
19. "Fossil: Fossil Performance". Fossil-scm.org. August 23, 2009. 29. Adam Kennedy. "DBD::SQLite - Self-contained RDBMS in a DBI
Retrieved September 12, 2009. Driver". Metacpan.org. Retrieved 2017-01-06.
20. "How SQLite Is Tested". SQLite.org. Retrieved September 12, 2009. 30. "PySqlite – The Trac Project". Trac.edgewall.org. 2015-12-31.
21. "SQLite Manager :: Add-ons for Firefox". Addons.mozilla.org. 2015- Retrieved 2017-01-06.
02-28. Retrieved 2017-01-06. 31. "Continue: Web Applications in Racket - 15 Using an SQL
22. "Skype client using SQLite?". Mail-archive.com. August 28, 2007. database". docs.racket-lang.org. Retrieved 2017-02-24.
Retrieved June 14, 2010. 32. "Archived copy". Archived from the original on 2010-04-17.
23. "Show Download History List of All Files Ever Downloaded Within Retrieved 2010-04-03. {{cite web}}: Unknown parameter
Mac OS X". Osxdaily.com. 2012-07-12. Retrieved 2017-01-06. |deadurl= ignored (|url-status= suggested) (help)
Sources
Allen, Grant; Owens, Mike (November 5, 2010). The Definitive Guide to SQLite (2nd ed.). Apress. p. 368. ISBN 1-4302-3225-0.
Kreibich, Jay A. (August 17, 2010). Using SQLite (1st ed.). O'Reilly Media. p. 528. ISBN 0-596-52118-9.
van der Lans, Rick F. (September 7, 2009). The SQL Guide to SQLite (1st ed.). lulu.com. p. 542. ISBN 0-557-07676-5.
Newman, Chris (November 9, 2004). SQLite (Developer's Library) (1st ed.). Sams. p. 336. ISBN 0-672-32685-X.
Downloading and using
Downloading
All tools as well as the C-sources and HTML-documentation you need to install sqlite3 on your computer are packed together in an archiveFile on SQLite's
downloadPage (https://sqlite.org/download.html).
In following files vv stands for version two digits following "3." uu=update (after ".vv.") and occasionally pp (usually 00) after ".uu."
Tools are stored in one or more files depending on your Operating System:
Android
sqlite-android-3vvuupp.aar (https://sqlite.org/2020/sqlite-android-3310100.aar)
Linux
sqlite-tools-linux-x86-3vvuupp.zip (https://sqlite.org/2020/sqlite-tools-linux-x86-3310100.zip)
Mac OS X (x86)
sqlite-tools-osx-x86-3vvuupp.zip (https://sqlite.org/2020/sqlite-tools-osx-x86-3310100.zip)
Windows
sqlite-tools-win32-x86-3vvuupp.zip (https://sqlite.org/2020/sqlite-tools-win32-x86-3310100.zip)
zipped 32-bit DLL (x86) for SQLite3vvuupp (https://sqlite.org/2020/sqlite-dll-win32-x86-3310100.zip)
zipped 64-bit DLL (x86) for SQLite3vvuupp (https://sqlite.org/2020/sqlite-dll-win64-x64-3310100.zip)
Windows Phone 8
Phone 8.0 VSIX package with an extension SDK VS2012 for SQLite3vvuupp (https://sqlite.org/2020/sqlite-wp80-winrt-3310100.vsix)
Phone 8.1 VSIX package with an extension SDK VS2013 for SQLite3vvuupp (https://sqlite.org/2020/sqlite-wp81-winrt-3310100.vsix)
Windows Runtime
Visual Studio 2012 VSIX package with an extension SDK WinRT for SQLite3vvuupp (https://sqlite.org/2020/sqlite-winrt-3310100.vsix)
Visual Studio 2013 VSIX package with an extension SDK WinRT 8.1 for SQLite3vvuupp (https://sqlite.org/2020/sqlite-winrt81-3310100.vsix)
.NET
source code and binaries of SQLite for .NET (http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki)
Sources
Amalgamation of SQLite3vvuupp (https://sqlite.org/2020/sqlite-amalgamation-3310100.zip)
Amalgamation of SQLite3vvuupp + (https://sqlite.org/2020/sqlite-autoconf-3310100.tar.gz)TCL-configuration
Alternative sources
not recommended
Beta sources
Prerelease tarball (https://sqlite.org/snapshot/sqlite-snapshot-202003260029.tar.gz) of 3.32.0 on 26 March 2020
Documentation (HTML)
Documentation as a bundle of static files to read in a webBrowser (https://sqlite.org/2020/sqlite-doc-3310100.zip)
Using
After unzipping and moving the content of the folder(s) under a directory in your %PATH% (or $PATH) you can use the executable sqlite3 from anywhere
within your computer system.
Questions?
For questions, requests etcetera there is a forum (https://sqlite.org/forum/forummain).
SQL standard divergences
Types
SQLite is dynamically typed. This means that as you fill columns the affinity depends on the content. Four data types exist except NULL: BLOB, INTEGER,
REAL (aka FLOAT) and TEXT.[1]
References
1. Result Values From A Query (https://www.sqlite.org/c3ref/column_blob.html) sqlite.org, SQLite C Interface, visited 7 May 2019
Release3
Before reading this you're supposed to have downloaded and unpacked in your PATH the latest version of the executable command line interface sqlite3 for
your operating system.
CommandLine
Usage: sqlite3 [OPTIONS] FILENAME [SQL]
FILENAME is the name of an SQLite database. A new database is created if the file does not previously exist.
SQL is a dot-, SQL-command or Pragma. OPTIONS include:
DotCommands
.archive OPT... FILE... Manage .headers (on,off) Turn display of headers on or off .read FILENAME Execute SQL in
SQL archives a .help ?REGEX? Show this message (or only REGEX)h FILENAME
.auth (ON,OFF) Show authorizer .recover Recover data from corrupt db
.import OPT... FILE TABLE Import FILE > TABLE i
callbacks .restore ?DB? FILE DB content (dflt
.imposter INDEX TABLE Create imposter table TABLE on "main") from FILE
.backup ?DB? FILE Backup DB index INDEX
(default "main") to FILE .save FILE Write in-memory database
.bail (on,off) Stop after hitting an .indexes ?TABLE? Show names of all indexest into FILE
error. Default OFF .limit ?LIMIT? ?VAL? Display or change the value of an .scanstats (on,off)
.binary (on,off) Turn binary output SQLITE_LIMIT sqlite3_stmt_scanstatus() metrics
on or off. Default OFF .lint OPTIONS Report potential schema issues. .schema ?PATTERN? Show the
.cd DIRECTORY Change the .load FILE ?ENTRY? Load an extension library CREATE statements matching
working DIRECTORY .log (FILE,off) Turn logging on or off. FILE can be PATTERNs1
.changes (on,off) Show number of stderr/stdout .selftest ?--init? Run tests defined in
rows changed by SQL .mode MODE ?TABLE? Set output modem for tablet the SELFTEST table
.check GLOB Fail if output since .nullvalue STRING Use STRING in place of NULL values .separator COL ?ROW? Change
.testcase does not match separator for column and optionally
.nonce STRING disable safe mode for one command
.clone NEWDB Clone data into rows2
.once (-e,-x,FILE) Output for the next SQL command only
NEWDB from the existing .sha3sum ?OPTIONS...? Compute a
database to FILEe
SHA3 hash of database content
.connection (close #) Open/close .open ?OPTIONS? ?FILE? Close existing database and
.shell CMD ARGS... Run CMD
auxiliary database reopen FILEo ARGS... in OS shell (=sy)
.databases Show names and files .output ?FILE? Send output to FILE or stdout .show Show the current values for
of attached databases .parameter CMD ... Manage SQL parameter bindingsp various settings
.dbconfig ?op? ?val? .print STRING... Print literal STRING .stats (on,off) Show stats or turn stats
Show/change sqlite3_db_config() on or off
.progress N Invoke progress handler after every N
options .system CMD ARGS... Run CMD
opcodes
.dbinfo ?DB? Show status ARGS... in OS shell (=.sh)
.prompt MAIN CONTINUE Replace the standard prompts
information about the database .tables ?TABLE? List names of
.quit Quit (=.ex) this program
.dump ?TABLE? ... Dump the tablest1
database in an SQL text formatt ------ screenshot from: ------ .testcase NAME Begin redirecting
.echo (on,off) Turn command https://en.wikibooks.org/wiki/SQLite/Release3#DotCommands output to 'testcase-out.txt'
echo on or off
.testctrl CMD ... Run
.eqp (on,off,trigger,full) sqlite3_test_control() opst2
En-/disable automatic EXPLAIN
QUERY PLAN .timeout MS Try opening locked tables
for MS milliseconds
.excel Display the output of next
command in a spreadsheet .timer (on,off) Turn SQL timer on or off
.exit ?CODE? Exit (=.q) this .trace (FILE,off) Output each SQL
program (returning CODE) statement as it is run
.expert EXPERIMENTAL. Indexes .version Show detailed SQLite version
for spec. queries info
.explain (on,off,auto) Explain .vfsinfo ?AUX? Information about the
format def. auto top-level VFS
.filectrl CMD ... Run .vfslist List all available VFSes
sqlite3_file_control() operationsf .vfsname' ?AUX? Print the name of
.fullschema ?--indent? Show the VFS stack
schema and the content of .width NUM1 ... Set col widths for
sqlite_stat tables "column" modew
D-notes
a .ar [OPTION...] [FILE...] The .ar command manages sqlar f CMD ... may be size_limit [LIMIT]; chunk_size SIZE;
archives.
persist_wal [BOOLEAN]; psow [BOOLEAN]; tempfilename
has_moved; lock_timeout MILLISEC
Examples: h only show commands matching REGEX e.g. p*=starting with p
i option to skip first line; --skip 1
.ar -cf archive.sar foo bar # Create archive.sar from files foo and l lintOptions: fkey-indexes Find missing foreign key indexes
bar
.ar -tf archive.sar # List members of archive.sar m mode: ascii delimited by 0x1F and 0x1E; csv Comma-separated
.ar -xvf archive.sar # Verbosely extract files from values;
archive.sar
column Left-aligned columns (See .width); html HTML <table> code;
insert SQL insert statements for TABLE; line One value per line
Each command line must feature exactly one command option: list delimited by "|"; quote Escape answers as for SQL
tabs Tab-separated values; tcl TCL list elements
o openOptions: --new option starts with an empty file
-c, --create Create a new archive
-u, --update Update or add files to an existing archive Other options: --readonly --append --zip
-t, --list List contents of archive p paramCMDs:
-x, --extract Extract files from archive
clear Erase all bindings
init Initialize the TEMP table that holds bindings
And zero or more optional options:
list List the current parameter bindings
set PARAMETER VALUE Given SQL parameter PARAMETER a
-v, --verbose Print each filename as it is processed value of VALUE
-f FILE, --file FILE Operate on archive FILE (default is current
db)
PARAMETER should start with '$', ':', '@', or '?'
-a FILE, --append FILE Operate on FILE opened using the apndvfs VFS unset PARAMETER Remove PARAMETER from the binding
-C DIR, --directory DIR Change to directory DIR to read/extract table
files s1 schemaOption: --indent for pretty-printing
-n, --dryrun Show the SQL that would have occurred
s2 separator for both the output mode and .import:
t1 If TABLE specified, only dump tables matching LIKE pattern
See also: sar-support (https://sqlite.org/cli.html#_sql_used_to_imple
TABLE.
ment_sqlite_archive_operations_) t2 CMD ... may be always BOOLEAN; assert BOOLEAN; byteorder;
imposter SCHEMA ON/OFF ROOTPAGE; internal_functions
e
or invoke system text editor (-e) or spreadsheet (-x) on the output. BOOLEAN; localtime_fault BOOLEAN
never_corrupt BOOLEAN; optimizations DISABLE-MASK;
pending_byte OFFSET; prng_reset;
prng_restore; prng_save; reserve BYTES-OF-RESERVE
w Negative values right-justify
KeyWords
If one wants to use one of following words as an identifier the word needs to be enclosed in two double quotes '"', backquotes '`' or '[' and ']'.[1]
Pragmas
Pragmas are special commands to show or [change] behavior of open database[schema](s)