SQLite3

is a simple high-level wrapper around SQLite3. It doesn’t intend to wrap everything, but it tries to be useful.

Installation

(load "https://veitheller.de/git/carpentry/sqlite3@0.0.4")

Usage

The module SQLite3 provides facilities for opening, closing, and querying databases.

(load "https://veitheller.de/git/carpentry/sqlite3@0.0.4")

; opening DBs can fail, for the purposes of this example we
; ignore that
(defn main []
  (let-do [db (Result.unsafe-from-success (SQLite3.open "db"))]
    ; Let's make sure our table is there
    (ignore
       (SQLite3.query &db
                      "CREATE TABLE IF NOT EXISTS mytable (name TEXT, age INT)"
                      &[]))

    ; we can prepare statements
    (ignore
       (SQLite3.query &db
                      "INSERT INTO mytable VALUES (?1, ?2);"
                      &[(to-sqlite3 @"Carp") (to-sqlite3 4)]))

    ; and query things
    (println* &(SQLite3.query &db "SELECT * from mytable;" &[]))
    (SQLite3.close db)))

Because open and query return Result types, we could also use combinators!

SQLite

doc-stub

a

is the opaque database type. You’ll need one of those to query anything.

It can be obtained by using open.

Type

module

Module

represent all the SQLite types we can represent.

The constructors are Null, Integer, Floating, Text, and Blob. Most primitive Carp types can be casted to appropriate SQLite types by using the to-sqlite3 interface.

close

external

(Fn [SQLite] ())

closes a database.

open

defn

(Fn [(Ref String a)] (Result SQLite String))

                    (open s)
                

opens a database with the filename s.

If it fails, we return an error message using Result.Error.

query

defn

(Fn [(Ref SQLite a), (Ref String b), (Ref (Array Type) c)] (Result (Array (Array Type)) String))

                    (query db s p)
                

queries the database db using the query s and the parameters p.

If it fails, we return an error message using Result.Error.