Exqlite

Build Status Hex Package Hex Docs

An Elixir SQLite3 library.

If you are looking for the Ecto adapater, take a look at the Ecto SQLite3 library.

Documentation: https://hexdocs.pm/exqlite Package: https://hex.pm/packages/exqlite

Caveats

  • Prepared statements are not cached.
  • Prepared statements are not immutable. You must be careful when manipulating statements and binding values to statements. Do not try to manipulate the statements concurrently. Keep it isolated to one process.
  • Simultaneous writing is not supported by SQLite3 and will not be supported here.
  • All native calls are run through the Dirty NIF scheduler.
  • Datetimes are stored without offsets. This is due to how SQLite3 handles date and times. If you would like to store a timezone, you will need to create a second column somewhere storing the timezone name and shifting it when you get it from the database. This is more reliable than storing the offset as +03:00 as it does not respect daylight savings time.

Installation

defp deps do
  {:exqlite, "~> 0.5.10"}
end

Configuration

config :exqlite, default_chunk_size: 100
  • default_chunk_size - The chunk size that is used when multi-stepping when not specifying the chunk size explicitly.

Usage

The Exqlite.Sqlite3 module usage is fairly straight forward.

# We'll just keep it in memory right now
{:ok, conn} = Exqlite.Sqlite3.open(":memory:")

# Create the table
:ok = Exqlite.Sqlite3.execute(conn, "create table test (id integer primary key, stuff text)");

# Prepare a statement
{:ok, statement} = Exqlite.Sqlite3.prepare(conn, "insert into test (stuff) values (?1)")
:ok = Exqlite.Sqlite3.bind(conn, statement, ["Hello world"])

# Step is used to run statements
:done = Exqlite.Sqlite3.step(conn, statement)

# Prepare a select statement
{:ok, statement} = Exqlite.Sqlite3.prepare(conn, "select id, stuff from test");

# Get the results
{:row, [1, "Hello world"]} = Exqlite.Sqlite3.step(conn, statement)

# No more results
:done = Exqlite.Sqlite3.step(conn, statement)

Why SQLite3

I needed an Ecto3 adapter to store time series data for a personal project. I didn't want to go through the hassle of trying to setup a postgres database or mysql database when I was just wanting to explore data ingestion and some map reduce problems.

I also noticed that other SQLite3 implementations didn't really fit my needs. At some point I also wanted to use this with a nerves project on an embedded device that would be resiliant to power outages and still maintain some state that ets can not afford.

Under The Hood

We are using the Dirty NIF scheduler to execute the sqlite calls. The rationale behind this is that maintaining each sqlite's connection command pool is complicated and error prone.

Contributing

Feel free to check the project out and submit pull requests.