0891 Learning Sqlite
0891 Learning Sqlite
0891 Learning Sqlite
#sqlite
Table of Contents
About 1
Versions 2
Examples 2
Installation 2
Documentation 2
Introduction 3
Examples 3
Remarks 4
Examples 4
TYPEOF function 4
Using booleans 4
Date/time types 5
ISO8601 strings 5
Unix timestamps 5
unsupported formats 6
Remarks 7
Examples 7
Remarks 8
Examples 8
Executing a Statement 8
Credits 11
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: sqlite
It is an unofficial and free sqlite ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official sqlite.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with sqlite
Versions
3.0 2004-06-18
Examples
Installation
SQLite is a C library that is typically compiled directly into the application by downloading the
source code of the latest version, and adding the sqlite3.c file to the project.
Many script languages (e.g., Perl, Python, Ruby, etc.) and frameworks (e.g., Android) have
support for SQLite; this is done with a built-in copy of the SQLite library, which does not need to be
installed separately.
For testing SQL, it might be useful to use the command-line shell (sqlite3 or sqlite3.exe). It is
already shipped with most Linux distributions; on Windows, download the precompiled binaries in
the sqlite-tools package, and extract them somewhere.
Documentation
SQLite already has extensive documentation, which should not be duplicated here.
https://riptutorial.com/ 2
Chapter 2: Command line dot-commands
Introduction
The sqlite3 command-line shell implements an additional set of commands (which are not
available in programs that use the SQLite library). Official documentation: Special commands to
sqlite3
Examples
Exporting and importing a table as an SQL script
The output file needs to be defined with .output prior to using .dump; otherwise, the text is just
output to the screen.
https://riptutorial.com/ 3
Chapter 3: Data types
Remarks
official documentation: Datatypes In SQLite Version 3
Examples
TYPEOF function
Using booleans
sqlite> SELECT 2 + 2 = 4;
1
sqlite> SELECT 'a' = 'b';
0
sqlite> SELECT typeof('a' = 'b');
integer
https://riptutorial.com/ 4
> INSERT INTO Test VALUES ('xxx', 'xxx', 'xxx', 'xxx', 'xxx');
> SELECT * FROM Test;
1 1 1 1 1
xxx xxx xxx xxx xxx
To enforce types, you have to add a constraint with the typeof() function:
(If such a column should be NULLable, you have to explicitly allow 'null'.)
Date/time types
ISO8601 strings
The built-in keywords CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP return strings in ISO8601
format:
The julianday() function converts any supported date/time value into a Julian day number:
https://riptutorial.com/ 5
Unix timestamps
The built-in date/time functions can interpret numbers as Unix timestamps with the unixepoch
modifier:
The strftime() function can convert any supported date/time value into a Unix timestamp:
unsupported formats
It would be possible to store date/time values in any other format in the database, but the built-in
date/time functions will not parse them, and return NULL:
https://riptutorial.com/ 6
Chapter 4: PRAGMA Statements
Remarks
The SQLite documentation has a reference of all PRAGMA statements.
Examples
PRAGMAs with permanent effects
Most PRAGMA statements affect only the current database connection, which means that they
have to be re-applied whenever the database has been opened.
However, the following PRAGMAs write to the database file, and can be executed at any time (but
in some cases, not inside a transaction):
• application_id
• journal_mode when enabling or disabling WAL mode
• schema_version
• user_version
• wal_checkpoint
The following PRAGMA settings set properties of the database file which cannot be changed after
creation, so they must be executed before the first actual write to the database:
For example:
https://riptutorial.com/ 7
Chapter 5: sqlite3_stmt: Prepared Statement
(C API)
Remarks
official documentation: Prepared Statement Object
Examples
Executing a Statement
A prepared statement object must be cleaned up with sqlite3_finalize(). Do not forget this in case
of an error.
If parameters are used, set their values with the sqlite3_bind_xxx() functions.
const char *sql = "INSERT INTO MyTable(ID, Name) VALUES (?, ?)";
sqlite3_stmt *stmt;
int err;
err = sqlite3_step(stmt);
if (err != SQLITE_DONE) {
printf("execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return /* failure */;
}
sqlite3_finalize(stmt);
return /* success */;
A SELECT query is executed like any other statement. To read the returned data, call
sqlite3_step() in a loop. It returns:
https://riptutorial.com/ 8
• SQLITE_DONE: if there are no more rows, or
• any error code.
If a query does not return any rows, the very first step returns SQLITE_DONE.
To read the data from the current row, call the sqlite3_column_xxx() functions:
for (;;) {
err = sqlite3_step(stmt);
if (err != SQLITE_ROW)
break;
if (err != SQLITE_DONE) {
printf("execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return /* failure */;
}
sqlite3_finalize(stmt);
return /* success */;
After a statement was executed, a call to sqlite3_reset() brings it back into the original state so that
it can be re-executed.
Typically, while the statement itself stays the same, the parameters are changed:
const char *sql = "INSERT INTO MyTable(ID, Name) VALUES (?, ?)";
sqlite3_stmt *stmt;
int err;
for (...) {
sqlite3_bind_int (stmt, 1, ...); /* ID */
https://riptutorial.com/ 9
sqlite3_bind_text(stmt, 2, ...); /* name */
err = sqlite3_step(stmt);
if (err != SQLITE_DONE) {
printf("execution failed: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return /* failure */;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
return /* success */;
https://riptutorial.com/ 10
Credits
S.
Chapters Contributors
No
Command line dot- CL., e4c5, James Toomey, Lasse Vågsæther Karlsen,
2
commands ravenspoint, Thinkeye
PRAGMA
4 CL., springy76
Statements
sqlite3_stmt:
5 Prepared Statement CL.
(C API)
https://riptutorial.com/ 11