| Did you know ... | Search Documentation: |
| Pack swiplite -- SQLITEDOC.md |
SQLite, like all other relational database vendors, has a
specific set of features and limitations. Here we discuss how
those are exposed through library(sqlite).
SQLite can be used by a multi-threaded application.
SQLite uses flexible typing. You can declare the types of columns in the `CREATE TABLE` statement, and SQLite will attempt to transform the input data to the preferred datatype of the column. However, any value in SQLite can have any storage class, regardless of the declared column type. In fact, column type declarations are not mandatory and can be omitted. This is a valid table declaration for a key-value store:
CREATE TABLE attribute(name TEXT PRIMARY KEY, value ) WITHOUT ROWID;
Values stored in the SQLite database have one of five storage classes.
The interface of library(sqlite) defines a two-way mapping of
Prolog types to SQLite. The purpose is to enable saving and
retrieving arbitrary Prolog terms to an SQLite database.
There are currently two possible ways to add values to the
SQLite database: using a literal in a SQL statement through
sqlite_prepare/3, or using a bind variables through
sqlite_bind/2. The only way to retrieve values is through
a SELECT statement with one of sqlite_one/2, sqlite_many/4,
and sqlite_row/2.
If we would prepare a statement and execute it:
sqlite_prepare(DB,
"INSERT INTO TABLE t VALUES ( 'foo', 12, 3.5 )", S),
sqlite_do(S)
In this case, the SQLite affinity rules apply. This means that for example, an integer literal outside of the range for an 8-byte signed integer will be represented as a REAL internally. The original integer value is lost and cannot be retrieved.
?- setup_call_cleanup(
sqlite_open(foo, DB, [mode(write),memory(true)]),
( sqlite_command(DB, "create table kv ( k text primary key, v ) without rowid"),
sqlite_command(DB, "insert into kv values ( 'foo', 9223372036854775808 )"),
sqlite_query(DB, "select * from kv", Row)
),
sqlite_close(DB)).
Row = row("foo", 9223372036854776000.0).
In a prepared SQL statement with parameters, the Prolog term used in the bind variable determines the conversion to a SQLite value. This is the current mapping:
| Prolog term type | `sqlite3_bind_*` function |
|---|---|
| [] % empty list | null |
| atom | text |
| string | text |
| list of codes | text |
| integer | int64 (throws on bigint) |
| float | double |
| var | (throws type_error) |
| compound | (throws type_error) |
SQLite column types as obtained with
sqlite3_column_type()
are mapped to the following Prolog types, or an error is thrown:
sqlite3_column_type | `PL_put_*` function |
|---|---|
| INTEGER | integer |
| FLOAT | float |
| TEXT | chars(PL_STRING) |
| NULL | nil |
| BLOB | (type_error) |
Foreign keys in SQLite must be enabled explicitly. First, the library must be compiled with neither of these two compile options defined:
In addition, for each connection, the application must enable foreign keys
with PRAGMA foreign_keys.
The PRAGMA can be used to check if the currently used version of SQLite supports foreign keys. This query:
PRAGMA foreign_keys;
... will not return rows if the SQLite version does not support foreign keys. It will return 0 or 1 to indicate that foreign key constraints are currently disabled (0) or enabled (1). It can be switched on and off with:
PRAGMA foreign_keys = ON; PRAGMA foreign_keys = OFF;
This library makes the following design decisions:
SQLite provides a couple of mechanisms for introspection. There is the schema table; there are also a few SQLite pragmas that return information on the tables in the schema:
In addition, the convenience predicate sqlite_schema/2 returns
the sql column of the sqlite_schema table.
SQLite PRAGMAs that have a result set can be accessed as if they were Select statements.
The library provides access to the following functions:
Those are implemented in sqlite_status/4, sqlite_db_status/5, and sqlite_stmt_status/4.
There are now bindings for:
At some point I might need bindings for sqlite3_config() and
sqlite3_db_config().