View Source sqlite (sqlite v2.0.0)

sqlite3 NIF bindings for Erlang.

See query/3 for the data type conversion rules.

All APIs of this module provide extended error information for the shell. This information is also accessible programmatically using 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 resource reference.

SQLite connection status

SQLite connection lookaside memory usage

SQLite connection pager cache memory usage

Erlang type allowed to be used in SQLite bindings.

Positional and named parameters.

Prepared statement creation flags

Prepared statement reference.
Backup progress callback. Must return 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

Backs up the main database of the source to the destination.

Runs a backup with the options specified

Binds arguments to a prepared statement.

Clears bindings of the 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.

Returns runtime statistics for the prepared statement.

Interrupts the query running on this connection.

EXPERIMENTAL: monitor updates happening through the connection.

Open the connection to a specified SQLite database.

Creates a prepared statement.

Runs an SQL query using specified connection

Resets the prepared statement, but does not clear bindings
Returns connection statistics.

Evaluates the prepared statement.

Evaluates the prepared statement.

Returns SQLite system information (memory usage, page cache usage, allocation statistics)

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 default
  • to: destination database name, takes from value as the default
  • stop: backup step, pages. Default is 64
  • progress: 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].

Example of an in-memory database shared between multiple connections:
  Db1 = sqlite:open("file::mem:", #{flags => [shared, memory, uri]}),
  Db2 = sqlite:open("file::mem:", #{flags => [shared, memory, uri]}),
  • memory: enables SQLITE_OPEN_MEMORY flag
  • uri: enables SQLITE_OPEN_URI flag
  • shared: enables SQLITE_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 is read_write_create
  • flags: SQLite connection flags specification
  • busy_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().
SQLite connection resource 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 of SQLITE_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 of SQLITE_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 of SQLITE_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 of SQLITE_DBSTATUS_LOOKASIDE_USED)
  • max: high watermark of SQLITE_DBSTATUS_LOOKASIDE_USED
  • hit: number of malloc attempts that were satisfied using lookaside memory (high watermark of SQLITE_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 of SQLITE_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 of SQLITE_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 of SQLITE_DBSTATUS_CACHE_USED)
  • shared: current value of SQLITE_DBSTATUS_CACHE_USED_SHARED
  • hit: the number of pager cache hits that have occurred (current value of SQLITE_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 of SQLITE_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 of SQLITE_DBSTATUS_CACHE_SPILL
-type parameter() ::
    integer() | float() | binary() | string() | iolist() | {binary, binary()} | undefined.
Erlang type allowed to be used in SQLite bindings.
-type parameters() :: [parameter()] | #{atom() | iolist() | pos_integer() => parameter()}.

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 times
  • no_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().
Prepared statement reference.
-type progress_callback() ::
    fun((Remaining :: non_neg_integer(), Total :: non_neg_integer()) ->
            ok | {ok, NewStep :: pos_integer()} | stop).
Backup progress callback. Must return 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. See sqlite3_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

See sqlite3_status in the SQLite reference for more details about exported statistics.

Link to this section Functions

Link to this function

backup(Source, Destination)

View Source
-spec backup(Source :: connection(), Destination :: connection()) -> ok.
Backs up the main database of the source to the destination.
Link to this function

backup(Source, Destination, Options)

View Source
-spec backup(Source :: connection(), Destination :: connection(), Options :: backup_options()) ->
          ok | stop.

Runs a backup with the options specified

Returns ok if the backup was successful, or stop if the process was requested to stop.
Link to this function

bind(Prepared, Parameters)

View Source
-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.

See query/3 for types and bindings details.
-spec clear(prepared_statement()) -> ok.
Clears bindings of the prepared statement
-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.

Throws badarg 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 the CREATE TABLE statement. SQLite accepts any string as a type name, unless STRICT mode is used.
Link to this function

execute(Prepared, Parameters)

View Source
-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.

See 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. Use erl_error module to provide human-readable exception explanations.
Link to this function

get_last_insert_rowid(Connection)

View Source
-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.

See sqlite3_last_insert_rowid in the SQLite reference.
-spec info(prepared_statement()) -> statement_info().
Returns runtime statistics for the prepared statement.
-spec interrupt(connection()) -> ok.

Interrupts the query running on this connection.

WARNING: this function is unsafe to use concurrently with close/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().
Open the connection to a specified SQLite database.
Link to this function

prepare(Connection, Query)

View Source
-spec prepare(connection(), iodata()) -> prepared_statement().

Equivalent to prepare(Connection, Query, #{}).

Link to this function

prepare(Connection, Query, Options)

View Source
-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.

Running a single query/3 once is more efficient than preparing a statement, executing it once and discarding.
Link to this function

query(Connection, Query)

View Source
-spec query(connection(), iodata()) -> [row()].

Equivalent to query(Connection, Query, []).

Link to this function

query(Connection, Query, Parameter)

View Source
-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"}.

Named parameters are also supported:
  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. See unicode module for transformations between Unicode characters list and a binary
  • string(): accepts an Erlang string limited to iolist() type, stored as a TEXT
  • iolist(): accepts lists supported by erlang:iolist_to_binary/1, stored as a TEXT
  • integer(): accepts a 64-bit signed integer (BIGNUM is not supported), stored as an INTEGER
  • float(): accepts IEEE floating point number, stored as a FLOAT
  • undefined: 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.

BLOB is always returned as a binary. Note that wrapping in {binary, Bin} tuple is only necessary for parameters, to tell the actual binary from an UTF8 encoded string.
-spec reset(prepared_statement()) -> ok.
Resets the prepared statement, but does not clear bindings
-spec status(connection()) -> connection_status().
Returns connection statistics.
-spec step(prepared_statement()) -> busy | done | row().

Evaluates the prepared statement.

Returns done 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().
Returns SQLite system information (memory usage, page cache usage, allocation statistics)