The interface to the NIF implementation.
Summary
Functions
Resets a prepared statement and binds values to it.
Binds a blob value to a prepared statement.
Binds a float value to a prepared statement.
Binds an integer value to a prepared statement.
Binds a null value to a prepared statement.
Returns number of SQL parameters in a prepared statement.
Binds a text value to a prepared statement.
Cancel a running query: wake any busy handler sleep and interrupt VDBE execution.
Get the number of changes recently.
Closes the database and releases any underlying resources.
Disconnect from database and then reopen as an in-memory database based on the serialized binary.
Allow loading native extensions.
Executes an sql script. Multiple stanzas can be passed at once.
Interrupt a long-running query.
Opens a new sqlite database at the Path provided.
Once finished with the prepared statement, call this to release the underlying resources.
Resets a prepared statement.
Serialize the contents of the database to a binary.
Set an authorizer that denies specific SQL operations.
Set the busy timeout in milliseconds without destroying the custom busy handler.
Send log messages to a process.
Configure how often SQLite invokes the progress handler during statement execution.
Send data change notifications to a process.
Causes the database connection to free as much memory as it can. This is useful if you are on a memory restricted system.
Types
@type bind_value() :: NaiveDateTime.t() | DateTime.t() | Date.t() | Time.t() | number() | iodata() | {:blob, iodata()} | atom()
@type db() :: reference()
@type open_mode() :: :readwrite | :readonly | :nomutex
@type open_opt() :: {:mode, :readwrite | :readonly | [open_mode()]}
@type row() :: list()
@type statement() :: reference()
Functions
@spec bind( statement(), [bind_value()] | %{optional(String.t()) => bind_value()} | nil ) :: :ok
Resets a prepared statement and binds values to it.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?, ?, ?, ?, ?")
iex> Sqlite3.bind(stmt, [42, 3.14, "Alice", {:blob, <<0, 0, 0>>}, nil])
iex> Sqlite3.step(conn, stmt)
{:row, [42, 3.14, "Alice", <<0, 0, 0>>, nil]}
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT :42, @pi, $name, @blob, :null")
iex> Sqlite3.bind(stmt, %{":42" => 42, "@pi" => 3.14, "$name" => "Alice", :"@blob" => {:blob, <<0, 0, 0>>}, ~c":null" => nil})
iex> Sqlite3.step(conn, stmt)
{:row, [42, 3.14, "Alice", <<0, 0, 0>>, nil]}
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind(stmt, [42, 3.14, "Alice"])
** (ArgumentError) expected 1 arguments, got 3
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?, ?")
iex> Sqlite3.bind(stmt, [42])
** (ArgumentError) expected 2 arguments, got 1
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind(stmt, [:erlang.list_to_pid(~c"<0.0.0>")])
** (ArgumentError) unsupported type: #PID<0.0.0>
@spec bind_blob(statement(), non_neg_integer(), binary()) :: :ok
Binds a blob value to a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_blob(stmt, 1, <<0, 0, 0>>)
:ok
@spec bind_float(statement(), non_neg_integer(), float()) :: :ok
Binds a float value to a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_float(stmt, 1, 3.14)
:ok
@spec bind_integer(statement(), non_neg_integer(), integer()) :: :ok
Binds an integer value to a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_integer(stmt, 1, 42)
:ok
@spec bind_null(statement(), non_neg_integer()) :: :ok
Binds a null value to a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_null(stmt, 1)
:ok
@spec bind_parameter_count(statement()) :: non_neg_integer() | {:error, reason()}
Returns number of SQL parameters in a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?, ?")
iex> Sqlite3.bind_parameter_count(stmt)
2
@spec bind_text(statement(), non_neg_integer(), String.t()) :: :ok
Binds a text value to a prepared statement.
iex> {:ok, conn} = Sqlite3.open(":memory:", [:readonly])
iex> {:ok, stmt} = Sqlite3.prepare(conn, "SELECT ?")
iex> Sqlite3.bind_text(stmt, 1, "Alice")
:ok
Cancel a running query: wake any busy handler sleep and interrupt VDBE execution.
This is a superset of interrupt/1 — it sets a cancel flag that the busy and
progress handlers observe, and also calls sqlite3_interrupt(). After a
cancel, the connection can be reused normally.
Use this when a query might be blocked either inside SQLite bytecode execution or inside the busy handler waiting for a lock.
Get the number of changes recently.
Note: If triggers are used, the count may be larger than expected.
Closes the database and releases any underlying resources.
Disconnect from database and then reopen as an in-memory database based on the serialized binary.
Allow loading native extensions.
Executes an sql script. Multiple stanzas can be passed at once.
Interrupt a long-running query.
This calls sqlite3_interrupt() and is effective while SQLite is actively
executing a statement. It does not wake the custom busy handler while the
connection is sleeping and waiting on a lock. Use cancel/1 when you need
to abort both statement execution and busy waits.
Warning
If you are going to interrupt a long running process, it is unsafe to call
close/1 immediately after. You run the risk of undefined behavior. This
is a limitation of the sqlite library itself. Please see the documentation
https://www.sqlite.org/c3ref/interrupt.html for more information.
If close must be called after, it is best to put a short sleep in order to let sqlite finish doing its book keeping.
Opens a new sqlite database at the Path provided.
path can be ":memory" to keep the sqlite database in memory.
Options
:mode- use:readwriteto open the database for reading and writing ,:readonlyto open it in read-only mode or[:readonly | :readwrite, :nomutex]to open it with no mutex mode.:readwritewill also create the database if it doesn't already exist. Defaults to:readwrite. Note: [:readwrite, :nomutex] is not recommended.
Once finished with the prepared statement, call this to release the underlying resources.
This should be called whenever you are done operating with the prepared statement. If the system has a high load the garbage collector may not clean up the prepared statements in a timely manner and causing higher than normal levels of memory pressure.
If you are operating on limited memory capacity systems, definitely call this.
Resets a prepared statement.
Serialize the contents of the database to a binary.
Set an authorizer that denies specific SQL operations.
Accepts a list of action atoms to deny. Any SQL statement that triggers a denied action will fail with a "not authorized" error during preparation.
Pass an empty list to clear the authorizer.
Action atoms
:attach, :detach, :pragma, :insert, :update, :delete,
:create_table, :drop_table, :create_index, :drop_index,
:create_trigger, :drop_trigger, :create_view, :drop_view,
:alter_table, :reindex, :analyze, :function, :savepoint,
:transaction, :read, :select, :recursive,
:create_temp_table, :create_temp_index, :create_temp_trigger,
:create_temp_view, :drop_temp_table, :drop_temp_index,
:drop_temp_trigger, :drop_temp_view, :create_vtable, :drop_vtable
Examples
# Block ATTACH and DETACH (prevent cross-database reads)
:ok = Sqlite3.set_authorizer(conn, [:attach, :detach])
# Clear the authorizer
:ok = Sqlite3.set_authorizer(conn, [])
Set the busy timeout in milliseconds without destroying the custom busy handler.
Unlike PRAGMA busy_timeout (which internally calls sqlite3_busy_timeout()
and replaces any custom handler), this function only updates the timeout value
that the custom busy handler reads. This preserves the ability to cancel
busy waits via cancel/1.
A timeout of 0 makes lock contention fail immediately with SQLITE_BUSY.
Larger values let SQLite keep retrying until the timeout expires or the wait
is cancelled.
This is the low-level API behind the :busy_timeout connection option.
Send log messages to a process.
Each time a message is logged in SQLite a message will be sent to the pid provided as the argument.
The message is of the form: {:log, rc, message}, where:
rcis an integer result code or an extended result codemessageis a string representing the log message
See SQLITE_CONFIG_LOG and
"The Error And Warning Log" for more details.
Restrictions
- Only one pid can listen to the log messages at a time. If this function is called multiple times, only the last pid will receive the notifications
Configure how often SQLite invokes the progress handler during statement execution.
The default is 1000 virtual machine steps.
Values less than 1 disable the progress handler. Larger values reduce the
overhead of cancellation checks at the cost of slower response to cancel/1
and interrupt/1 while a query is running.
This is the low-level API behind the :progress_handler_steps connection
option.
Send data change notifications to a process.
Each time an insert, update, or delete is performed on the connection provided as the first argument, a message will be sent to the pid provided as the second argument.
The message is of the form: {action, db_name, table, row_id}, where:
actionis one of:insert,:updateor:deletedb_nameis a string representing the database name where the change took placetableis a string representing the table name where the change took placerow_idis an integer representing the unique row id assigned by SQLite
Restrictions
- There are some conditions where the update hook will not be invoked by SQLite. See the documentation for more details
- Only one pid can listen to the changes on a given database connection at a time. If this function is called multiple times for the same connection, only the last pid will receive the notifications
- Updates only happen for the connection that is opened. For example, there are two connections A and B. When an update happens on connection B, the hook set for connection A will not receive the update, but the hook for connection B will receive the update.
Causes the database connection to free as much memory as it can. This is useful if you are on a memory restricted system.
@spec transaction_status(db()) :: {:ok, :idle | :transaction}