SQLite in Livebook using DbConnection smart cell

I’m trying to use the DatabaseConnection smart cell in a livebook to query a SQLite database.

When I connecti it, it says:

 {:ok, #PID<0.293.0>}

But If I try creating a query it fails with {:error, :invalid_connection} when I run:

 {:ok, statement} = Exqlite.Sqlite3.prepare(c2, "select * from table")

In fact it works if I create a connection manually:

 {:ok, c2} =Exqlite.Sqlite3.open("my.db")

But the connection is different - it’s a reference not a PID:

  {:ok, #Reference<0.4102545559.2337144860.146613>}

So how do I use the smartcell’s own connection? I’m sure I’m missing something trivial. With MySql it just works.

There are two different layers, the cell starts a “connection” process and you can use the Exqlite module functions to interact with the connection. The Exqlite.Sqlite3 module works on a specific reference available to the opening process.

So for your example, you want to use Exqlite.prepare (or just Exqlite.query). Note that Livebook also has “SQL query” cell with SQL editor, that does it for you : )

1 Like

Thank you - the handling of results seemed bit low-level… :wink: