sqlitex v1.7.1 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
Another esqlite parameter is :db_chunk_size.
This is a count of rows to read from native sqlite and send to erlang process in one bulk.
For example, the table mytable has 1000 rows. We make the query to get all rows with db_chunk_size: 500 parameter:
Sqlitex.query(db, "select * from mytable", db_chunk_size: 500)
in this case all rows will be passed from native sqlite OS thread to the erlang process in two passes.
Each pass will contain 500 rows.
This parameter decrease overhead of transmitting rows from native OS sqlite thread to the erlang process by
chunking list of result rows.
Please, decrease this value if rows are heavy. Default value is 5000.
If you in doubt what to do with this parameter, please, do nothing. Default value is ok.
config :sqlitex, db_chunk_size: 500 # if most of the database rows are heavy
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
Send a raw SQL statement to the database
A shortcut to Sqlitex.Query.query/3
A shortcut to Sqlitex.Query.query!/3
A shortcut to Sqlitex.Query.query_rows/3
A shortcut to Sqlitex.Query.query_rows!/3
Sets a PID to recieve notifications about table updates.
Link to this section Types
connection() View Source
sqlite_error()
View Source
sqlite_error() :: {:error, {:sqlite_error, charlist()}}
sqlite_error() :: {:error, {:sqlite_error, charlist()}}
string_or_charlist() View Source
Link to this section Functions
close(db, opts \\ [])
View Source
close(connection(), Keyword.t()) :: :ok
close(connection(), Keyword.t()) :: :ok
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]}
exec(db, sql, opts \\ [])
View Source
exec(connection(), string_or_charlist(), Keyword.t()) :: :ok | sqlite_error()
exec(connection(), string_or_charlist(), Keyword.t()) :: :ok | sqlite_error()
Send a raw SQL statement to the database
This function is intended for running fully-complete SQL statements. No query preparation, or binding of values takes place. This is generally useful for things like re-playing a SQL export back into the database.
open(path, opts \\ []) View Source
query(db, sql, opts \\ []) View Source
A shortcut to Sqlitex.Query.query/3
query!(db, sql, opts \\ []) View Source
A shortcut to Sqlitex.Query.query!/3
query_rows(db, sql, opts \\ [])
View Source
query_rows(connection(), String.t() | charlist(),
bind: [],
db_timeout: integer()
) :: {:ok, %{}} | sqlite_error()
query_rows(connection(), String.t() | charlist(), bind: [], db_timeout: integer() ) :: {:ok, %{}} | sqlite_error()
A shortcut to Sqlitex.Query.query_rows/3
query_rows!(db, sql, opts \\ [])
View Source
query_rows!(connection(), String.t() | charlist(),
bind: [],
db_timeout: integer()
) :: %{}
query_rows!(connection(), String.t() | charlist(), bind: [], db_timeout: integer() ) :: %{}
A shortcut to Sqlitex.Query.query_rows!/3
set_update_hook(db, pid, opts \\ [])
View Source
set_update_hook(connection(), pid(), Keyword.t()) :: :ok | {:error, term()}
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 | :deletetable-> charlist of the table name. Example:'posts'rowid-> internal immutable rowid index of the row. This is NOT theidorprimary keyof the row. See the official docs.