View Source sqlite (sqlite v2.0.0)
sqlite3 NIF bindings for Erlang.
See query/3
for the data type conversion rules.
erl_error
. try
sqlite:prepare(sqlite:open(""), "INSERT INTO kv (key, val) VALU1ES (2, 2)")
catch
Class:Reason:Stack ->
Formatted = erl_error:format_exception(Class, Reason, Stack),
io:format(lists:flatten(Formatted))
end.
Link to this section Summary
Types
Backup options
SQLite connections flags specification
SQLite connection options.
SQLite connection status
SQLite connection lookaside memory usage
SQLite connection pager cache memory usage
Positional and named parameters.
Prepared statement creation flags
ok
for backup to continue, {ok, Steps}` to change the steps amount. Return `stop
to stop the backup process. Throwing an exception also stops the process.Prepared statement statistics
Status information about the performance of SQLite
Functions
Runs a backup with the options specified
Binds arguments to a prepared statement.
Closes the connection.
EXPERIMENTAL: Stops monitoring previously monitored connection.
Returns column names and types for the prepared statement.
Runs the prepared statement with new parameters bound.
Finalises the prepared statement, freeing any resources allocated
Formats exception according to EEP-54.
Usually returns the ROWID of the most recent successful INSERT into a rowid table or virtual table on database connection.
Interrupts the query running on this connection.
EXPERIMENTAL: monitor updates happening through the connection.
Equivalent to open(FileName, #{}).
Equivalent to prepare(Connection, Query, #{}).
Creates a prepared statement.
Equivalent to query(Connection, Query, []).
Runs an SQL query using specified connection
Evaluates the prepared statement.
Evaluates the prepared statement.
Link to this section Types
-type backup_options() :: #{from => unicode:chardata(), to => unicode:chardata(), step => pos_integer(), progress => progress_callback()}.
Backup options
from
: source database name,<<"main">>
is the defaultto
: destination database name, takesfrom
value as the defaultstop
: backup step, pages. Default is 64progress
: progress callback to invoke after finishing the next step
-type connect_flag() :: memory | uri | shared.
SQLite connections flags specification
Normally used to specify an in-memory database: flags => [memory]
.
Db1 = sqlite:open("file::mem:", #{flags => [shared, memory, uri]}),
Db2 = sqlite:open("file::mem:", #{flags => [shared, memory, uri]}),
memory
: enablesSQLITE_OPEN_MEMORY
flaguri
: enablesSQLITE_OPEN_URI
flagshared
: enablesSQLITE_OPEN_SHAREDCACHE
flag
-type connect_options() :: #{mode => read_only | read_write | read_write_create, flags => [connect_flag()], busy_timeout => non_neg_integer()}.
SQLite connection options.
mode
: file open mode, the default isread_write_create
flags
: SQLite connection flags specificationbusy_timeout
: specifies SQLite busy timeout. NOTE: using this setting may easily exhaust all ERTS Dirty I/O schedulers. Prefer WAL mode instead
-type connection() :: reference().
-type connection_status() :: #{lookaside_memory => lookaside_memory(), pager_cache_memory => pager_cache_memory(), schema => integer(), statement => integer(), deferred_fks => integer()}.
SQLite connection status
schema
: the approximate number of bytes of heap memory used to store the schema for all databases associated with the connection (current value ofSQLITE_DBSTATUS_SCHEMA_USED
)statement
: the approximate number of bytes of heap and lookaside memory used by all prepared statements associated with the database connection (current value ofSQLITE_DBSTATUS_STMT_USED
)deferred_fks
: returns zero for the current value if and only if all foreign key constraints (deferred or immediate) have been resolved (current value ofSQLITE_DBSTATUS_DEFERRED_FKS
)
-type lookaside_memory() ::
#{used => integer(),
max => integer(),
hit => integer(),
miss_size => integer(),
miss_full => integer()}.
SQLite connection lookaside memory usage
used
: the current number of lookaside memory slots currently checked out (current value ofSQLITE_DBSTATUS_LOOKASIDE_USED
)max
: high watermark ofSQLITE_DBSTATUS_LOOKASIDE_USED
hit
: number of malloc attempts that were satisfied using lookaside memory (high watermark ofSQLITE_DBSTATUS_LOOKASIDE_HIT
)miss_size
: the number malloc attempts that might have been satisfied using lookaside memory but failed due to the amount of memory requested being larger than the lookaside slot size (high watermark ofSQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
)miss_full
: the number malloc attempts that might have been satisfied using lookaside memory but failed due to all lookaside memory already being in use (high watermark ofSQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
)
-type pager_cache_memory() ::
#{used => integer(),
shared => integer(),
hit => integer(),
miss => integer(),
write => integer(),
spill => {Current :: integer(), Max :: integer()}}.
SQLite connection pager cache memory usage
used
: the approximate number of bytes of heap memory used by all pager caches associated with the database connection (current value ofSQLITE_DBSTATUS_CACHE_USED
)shared
: current value ofSQLITE_DBSTATUS_CACHE_USED_SHARED
hit
: the number of pager cache hits that have occurred (current value ofSQLITE_DBSTATUS_CACHE_HIT
)miss
: the number of pager cache misses that have occurred (current value of SQLITE_DBSTATUS_CACHE_MISS)write
: the number of dirty cache entries that have been written to disk (current value ofSQLITE_DBSTATUS_CACHE_WRITE
)spill
: the number of dirty cache entries that have been written to disk in the middle of a transaction due to the page cache overflowing. Both values ofSQLITE_DBSTATUS_CACHE_SPILL
-type parameter() ::
integer() | float() | binary() | string() | iolist() | {binary, binary()} | undefined.
Positional and named parameters.
A list of parameters must match the expected number of parameters. A map of parameters may be used to define named parameters, or assign specific columns. When map key is an integer, it is treated as a column index.-type prepare_options() :: #{persistent => boolean(), no_vtab => boolean()}.
Prepared statement creation flags
persistent
: The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner that the prepared statement will be retained for a long time and probably reused many timesno_vtab
: The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler to return an error (error code SQLITE_ERROR) if the statement uses any virtual tables
-type prepared_statement() :: reference().
-type progress_callback() ::
fun((Remaining :: non_neg_integer(), Total :: non_neg_integer()) ->
ok | {ok, NewStep :: pos_integer()} | stop).
ok
for backup to continue, {ok, Steps}` to change the steps amount. Return `stop
to stop the backup process. Throwing an exception also stops the process.
-type row() :: tuple().
-type statement_info() ::
#{fullscan_step => integer(),
sort => integer(),
autoindex => integer(),
vm_step => integer(),
reprepare => integer(),
run => integer(),
filter_miss => integer(),
filter_hit => integer(),
memory_used => integer()}.
Prepared statement statistics
Performance counters for SQLite statement. Seesqlite3_stmt_status
function in SQLite reference.
-type system_info() ::
#{memory_used => {Cur :: integer(), Max :: integer()},
page_cache =>
{Size :: integer(),
Used :: integer(),
Max :: integer(),
Overflow :: integer(),
OverflowMax :: integer()},
malloc => {Size :: integer(), Count :: integer(), Max :: integer()},
version => binary()}.
Status information about the performance of SQLite
Seesqlite3_status
in the SQLite reference for more details about exported statistics.
Link to this section Functions
-spec backup(Source :: connection(), Destination :: connection()) -> ok.
-spec backup(Source :: connection(), Destination :: connection(), Options :: backup_options()) -> ok | stop.
Runs a backup with the options specified
Returnsok
if the backup was successful, or stop
if the process was requested to stop.
-spec bind(prepared_statement(), parameters()) -> ok.
Binds arguments to a prepared statement.
Does not reset the statement, and throws badarg
with SQL_MISUSE explanation if the statement needs to be reset first.
query/3
for types and bindings details.
-spec clear(prepared_statement()) -> ok.
-spec close(connection()) -> ok.
Closes the connection.
Ensures that all prepared statements are deallocated. Forcibly stops all running backups (both source and destination). Releases the underlying database file.
It is not necessary to call this function explicitly, as eventually all resources will be collected. However it might be useful if you need to unlock the database file immediately.
Throwsbadarg
if the connection is already closed.
-spec demonitor(reference()) -> ok.
EXPERIMENTAL: Stops monitoring previously monitored connection.
Does not flush messages that are already in transit. This API is experimental and may change in the future.-spec describe(prepared_statement()) -> [{Name :: binary(), Type :: binary()}].
Returns column names and types for the prepared statement.
SQLite uses dynamic type system. Types returned are column types specified in theCREATE TABLE
statement. SQLite accepts any string as a type name, unless STRICT
mode is used.
-spec execute(prepared_statement(), parameters()) -> [row()].
Runs the prepared statement with new parameters bound.
Resets the prepared statement, binds new parameters and steps until no more rows are available. Throws an error if at any step SQLITE_BUSY
was returned, see sqlite:connect_options()
for busy timeout handling.
query/3
for types and bindings details.
-spec finish(prepared_statement()) -> ok.
Finalises the prepared statement, freeing any resources allocated
The purpose of this function is to cancel statements that are executed halfway through, and it is desirable to stop execution and free all allocated resources.Formats exception according to EEP-54.
Used internally by the shell exception handler. Note that NIFs are not yet supported, and therefore exceptions are thrown by Erlang code. Useerl_error
module to provide human-readable exception explanations.
-spec get_last_insert_rowid(connection()) -> integer().
Usually returns the ROWID of the most recent successful INSERT into a rowid table or virtual table on database connection.
Seesqlite3_last_insert_rowid
in the SQLite reference.
-spec info(prepared_statement()) -> statement_info().
-spec interrupt(connection()) -> ok.
Interrupts the query running on this connection.
WARNING: this function is unsafe to use concurrently withclose/1
on the same connection.
-spec monitor(connection()) -> reference().
EXPERIMENTAL: monitor updates happening through the connection.
This function is intended for debugging INSERT, UPDATE and DELETE operations. The API is experimental and may change in the future without prior notice.
Only one process may monitor a connection. Subsequent monitor calls replace the previously set process.
Upon successful completion, calling process may start receiving messages of the following format: {Ref, Op, Database, Table, RowID}
Example:
{#Ref<0.1954006965.2226257955.79689>, insert, <<"main">>, <<"table">>, 123}
Ref
is the reference returned by monitor/1
call, Op
is one of insert
, update
or delete
operations, and RowID
is the SQLite ROWID.
-spec open(file:filename_all()) -> connection().
Equivalent to open(FileName, #{}).
-spec open(file:filename_all(), Options :: connect_options()) -> connection().
-spec prepare(connection(), iodata()) -> prepared_statement().
Equivalent to prepare(Connection, Query, #{}).
-spec prepare(connection(), iodata(), prepare_options()) -> prepared_statement().
Creates a prepared statement.
Use execute/2
to run the statement. Note that close/1
invalidates all prepared statements created using the specified connection.
By default, the prepared statement is not persistent. Note that if a single statement is going to be reused many times, it may prove useful to pass persistent
option set to true
.
query/3
once is more efficient than preparing a statement, executing it once and discarding.
-spec query(connection(), iodata()) -> [row()].
Equivalent to query(Connection, Query, []).
-spec query(connection(), iodata(), parameters()) -> [row()].
Runs an SQL query using specified connection
Query may contain placeholders, e.g. ?
, ?1
, ?2
. Number of placeholders must match the length of the parameters list. However for a map-based parameters, there is no such limitation, and it is supported to bind a single column using the following syntax: #{2 => "value"}
.
sqlite:query(Conn, "SELECT * FROM table WHERE key = :key", #{key => 1}).
Missing named parameters are treated as NULL.
Parameters are converted to sqlite storage classed using following type mappings:binary()
: accepts an Erlang binary stored as a TEXT in sqlite. Seeunicode
module for transformations between Unicode characters list and a binarystring()
: accepts an Erlang string limited toiolist()
type, stored as a TEXTiolist()
: accepts lists supported byerlang:iolist_to_binary/1
, stored as a TEXTinteger()
: accepts a 64-bit signed integer (BIGNUM is not supported), stored as an INTEGERfloat()
: accepts IEEE floating point number, stored as a FLOATundefined
: stored as NULL{blob, binary()}
: accepts an Erlang binary stored as a BLOB
Returns a list of database rows, or an empty list if no rows are expected to be returned (e.g. CREATE TABLE
statement). By default, all TEXT fields are returned as Erlang binary()
type for performance reasons.
{binary, Bin}
tuple is only necessary for parameters, to tell the actual binary from an UTF8 encoded string.
-spec reset(prepared_statement()) -> ok.
-spec status(connection()) -> connection_status().
-spec step(prepared_statement()) -> busy | done | row().
Evaluates the prepared statement.
Returnsdone
when an operation has completed. May return busy
, see SQLITE_BUSY
return code in the sqlite reference manual.
-spec step(prepared_statement(), pos_integer()) -> [row()] | {done, [row()]} | {busy, [row()]}.
Evaluates the prepared statement.
Returns either a list of rows when there are more rows available, or{done, Rows}` tuple with the list of the remaining rows. May return `{busy, Rows}
, requesting the caller to handle SQL_BUSY
according to sqlite recommendations.
-spec system_info() -> system_info().