sqlitex v1.6.0 Sqlitex View Source

Sqlitex gives you a way to create and query SQLite databases.

Basic Example

iex> {:ok, db} = Sqlitex.open(":memory:")
iex> Sqlitex.exec(db, "CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER)")
:ok
iex> Sqlitex.exec(db, "INSERT INTO t VALUES (1, 2, 3)")
:ok
iex> Sqlitex.query(db, "SELECT * FROM t")
{:ok, [[a: 1, b: 2, c: 3]]}
iex> Sqlitex.query(db, "SELECT * FROM t", into: %{})
{:ok, [%{a: 1, b: 2, c: 3}]}

Configuration

Sqlitex uses the Erlang library esqlite which accepts a timeout parameter for almost all interactions with the database. The default value for this timeout is 5000 ms. Many functions in Sqlitex accept a :db_timeout option that is passed on to the esqlite calls and also defaults to 5000 ms. If required, this default value can be overridden globally with the following in your config.exs:

config :sqlitex, db_timeout: 10_000 # or other positive integer number of ms

Link to this section Summary

Functions

Create a new table name where table_opts is a list of table constraints and cols is a keyword list of columns. The following table constraints are supported: :temp and :primary_key. Example

Sets a PID to recieve notifications about table updates.

Link to this section Types

Link to this type

connection() View Source
connection() :: {:connection, reference(), binary()}

Link to this type

sqlite_error() View Source
sqlite_error() :: {:error, {:sqlite_error, charlist()}}

Link to this type

string_or_charlist() View Source
string_or_charlist() :: String.t() | charlist()

Link to this section Functions

Link to this function

close(db, opts \\ []) View Source
close(connection(), Keyword.t()) :: :ok

Link to this function

create_table(db, name, table_opts \\ [], cols, call_opts \\ []) View Source

Create a new table name where table_opts is a list of table constraints and cols is a keyword list of columns. The following table constraints are supported: :temp and :primary_key. Example:

[:temp, {:primary_key, [:id]}]

Columns can be passed as:

  • name: :type
  • name: {:type, constraints}

where constraints is a list of column constraints. The following column constraints are supported: :primary_key, :not_null and :autoincrement. Example:

id: :integer, name: {:text, [:not_null]}

Link to this function

open(path, opts \\ []) View Source
open(charlist() | String.t(), Keyword.t()) ::
  {:ok, connection()} | {:error, {atom(), charlist()}}

Link to this function

query(db, sql, opts \\ []) View Source

Link to this function

query!(db, sql, opts \\ []) View Source

Link to this function

query_rows(db, sql, opts \\ []) View Source

Link to this function

query_rows!(db, sql, opts \\ []) View Source

Link to this function

set_update_hook(db, pid, opts \\ []) View Source
set_update_hook(connection(), pid(), Keyword.t()) :: :ok | {:error, term()}

Sets a PID to recieve notifications about table updates.

Messages will come in the shape of: {action, table, rowid}

  • action -> :insert | :update | :delete
  • table -> charlist of the table name. Example: 'posts'
  • rowid -> internal immutable rowid index of the row. This is NOT the id or primary key of the row. See the official docs.
Link to this function

with_db(path, fun, opts \\ []) View Source