(chibi sqlite3)

A high-level wrapper around sqlite3, with automatic resource management, iteration via fold and/or conversion to lists, and SQL statements as s-expressions.

Simple interface

(sqlite3-fold kons knil db stmt . vals)

The fundamental sqlite3 result iterator. Executes stmt-obj on db, binding vals first if given. Then iterates on the results, each time calling kons with two arguments, stmt and the current accumulator, which starts as knil. stmt-obj can be a prepared statement, a string to parse into a statement, or an SSQL sexp.

(sqlite3-select db stmt . vals)

Executes stmt on db, binding vals first if given. Returns all results as a list, where each row is a vector.

(sqlite3-do db stmt . vals)

Executes stmt on db, similar to sqlite3-exec but binding vals first if given. Returns an unspecified value.

(sqlite3-get db stmt . vals)

Executes stmt on db, binding vals first if given. Returns the first column of the first result.Example:
(let ((db (sqlite3-open ":memory:")))
  (sqlite3-do db '(create (table animals)
                          (columns (name (varchar 64))
                                   (color (varchar 64))
                                   (weight int))))
  (sqlite3-do db '(insert (into animals)
                          (columns name color weight)
                          (values #("cat" "black" 3)
                                  #("dog" "white" 5)
                                  #("pink" "elephant" 3000))))
  (sqlite3-select db '(select (columns name color)
                              (from animals)
                              (where (< weight 10))
                              (order name))))
=> (#("cat" "black") #("dog" "white"))

SQL as S-expressions

(ssql->sql ssql)

Utility to convert an "ssql" sexp-based SQL syntax to an SQL string, similar to the Chicken Scheme ssql egg.

(sqlite3-lambda db query body ...)

Syntax for a procedure of n arguments, to applied to the given query, and the body evaluated. If the query is of the form (select (columns ...) ...), then the names of the columns are bound with their corresponding values in body.

(sqlite3-loop db query ((var init step) ...) [result])

Syntax for a procedure of n arguments, to applied to the given query, which must be of the form (select (columns ...) ...). vars are initialized to their correspnding inits, and for each result of the query are updated to the steps, as with the do syntax. Within the steps, the named columns are bound to the SQL results as in sqlite3-lambda. When all rows have been processed, returns result, which defaults to the current values of all vars. Example:
(let ((db (sqlite3-open ":memory:")))
  (sqlite3-do db '(create (table animals)
                          (columns (name (varchar 64))
                                   (color (varchar 64))
                                   (weight int))))
  (sqlite3-do db '(insert (into animals)
                          (columns name color weight)
                          (values #("cat" "black" 3)
                                  #("dog" "white" 5)
                                  #("pink" "elephant" 3000))))
  (let ((f (sqlite3-loop db (select (columns name color)
                                    (from animals)
                                    (where (< weight ?))
                                    (order (desc name)))
             ((res '() (cons (string-append color " " name) res))))))
    (f 10)))
=> ("black cat" "white dog")

Dynamically Typed Binding Forms

(sqlite3-bind stmt i val)

Binds the ith argument of stmt to val.

(sqlite3-bind-all stmt . vals)

Binds the arguments of stmt to vals, in order.

(sqlite3-column stmt col)

Returns the colth column of the current result of stmt.

(sqlite3-columns stmt)

Returns all columns of the current result of stmt as a vector.

Low-level Bindings

Sqlite constants.A connection to an sqlite3 database. Closes on gc.A prepared sqlite3 statement. Finalizes on gc.

(sqlite3-errmsg sqlite3)

Return the last error message from the db.

(sqlite3-open string)

Open a connection to a new sqlite3 database. Note if string is ":memory:" opens an anonymous in-memory database.

(sqlite3-exec sqlite3 string)

Executes a single SQL string on the database.

(sqlite3-prepare sqlite3 string)

Returns a new prepared SQL statement from the string.

(sqlite3-bind-int sqlite3_stmt int int)

Bind an integer value to the given argument of the statement.

(sqlite3-bind-double sqlite3_stmt int double)

Bind an real value to the given argument of the statement.

(sqlite3-bind-text sqlite3_stmt int string)

Bind a string value to the given argument of the statement.Bind a bytevector value to the given argument of the statement.

(sqlite3-reset sqlite3_stmt)

Resets any bindings and results of the given statement.

(sqlite3-step sqlite3_stmt)

Advance to the next result of the statement. If a new or reset statement, executes it with the current bindings and advances to the first result.

(sqlite3-column-count sqlite3_stmt)

Returns the number of columns in the statement results.

(sqlite3-column-type sqlite3_stmt int)

Returns the SQL type of the ith columns of the statement results.

(sqlite3-column-int sqlite3_stmt int)

Returns the ith column of the current result as an integer.

(sqlite3-column-double sqlite3_stmt int)

Returns the ith column of the current result as a real.

(sqlite3-column-text sqlite3_stmt int)

Returns the ith column of the current result as a string.Returns the ith column of the current result as a bytevector.

(sqlite3-column-bytes sqlite3_stmt int)

Returns the size in bytes of the ith column of the current result.