SQLite: An interface to SQLite databases
_SQLite: An interface to SQLite databases_
By Jay McCarthy (jay at kenyamountain dot com)
Keywords: _database_, _io_, _sqlite_
I noticed many people asking for SQLite support and I wanted to learn the new FFI. So I took an afternoon and wrote this library.
This file contains the actual FFI code.
If you have a problem loading the sqlite library, you need to change one of the first lines to the commented out line. This seems to be a problem with the FFI library.
If you are changing _sqlite.ss_ you will probably need to look at this, but otherwise you won't.
This is the main file for the library. Require it like this:
> (require (planet "sqlite.ss" ("jaymccarthy" "sqlite.plt" 1)))
It provides the follow procedures:
> (open db-path) :: path -> db
Opens the SQLite database at ``db-path''.
> (close db) :: db -> void
Closes the database, db
> (exec db sql callback) :: db string (list-of-strings list-of-strings -> integer) -> void
Executes the sql with the given db, calling the callback with two lists, one of the column names and one of the column values, returning a status code. If the sql does not return results, the callback will not be called.
> (exec/ignore db sql) :: db string -> void
A wrapper around exec that provides a meaningless callback.
> (select db sql) :: db string -> list-of-lists-of-strings
Executes the sql with the given, collating the results in to a list where each element is a list of the columns values as strings.
> (prepare db sql) :: db string -> statement
Compiles the sql into a statement object for the given db. The sql may contain ``?'' to mark a parameter.
> (load-params statement . params) :: statement list-of-values -> void
Loads the params into the statement
> (step statement) :: statement -> list-of-values
Steps the statement to the next result, returning the column values as a list. (If the statement does not return results, this returns false.) (Use for SELECT)
> (step* statement) :: statment -> list-of-list-of-values
Runs steps until it is done. (Use this rather than select or exec when you want to use a placeholder (?) in the query and have SQLite do the quoting for you.)
> (run statement . params) :: statement list-of-strings -> void
Loads the params in the statement, then runs the statement. (If the statement returns reults, they are not available.) (Use for UPDATE and INSERT)
> (reset statement) :: statement -> void
Resets a statement for re-execution.
> (finalize statement) :: statement -> void
Finalizes a statement.
> (transaction db fail body ...) :: syntax
Executes the body within a transaction on the given db, with the fail symbol bound to a procedure that escapes from the body and fails the transaction.
> (transaction/lock db lock-type fail body ...) :: syntax
Like transaction, except that the transaction is locked as lock-type. Refer to the SQLite locking documentation  for explanation of different modes.
> (errmsg db) :: db -> string
Returns the message for the last error with the database.
> (changes-count db) :: db -> integer
Returns a count of how many changes have been made to the database in the current session.
> (total-changes-count db) :: db -> integer
Returns a count of how many changes have been made to the database since its creation.
See the code in the tests/ directory.
Notes and Limitations
I'm fairly certain that the memory problems are gone, but let me know if you find them.
I'd like to port the s-expr -> SQL portion of Schematics' SchemeQL project to be a general library that can be used with this library as well.
v2.0 :: August 17th, 2005
* Adding better error handling
* Supporting paths rather strings for db paths
* Returning void, rather than status-codes where errors are now caught by the library
* Adding 'reset'
v1.5 :: April 7th, 2005
* Added step*
v1.4 :: March 28th, 2005
* Fixing some contracts
* Added 'errmsg'
* Fixing transaction behaviour ("END" is actually a synonym for "COMMIT")
* Remove sqlite-oo.ss, it was a bad idea.
v1.3 :: March 25th, 2005
* Fixed require lines (Jacob Matthews)
* Fixed possible leak with lambdas passed directly to the FFI (Eli)
* Fixed position of finalizers (Eli)
v1.2 :: March 25th, 2005
* Added documentation
* Added sqlite-oo.ss module
v1.1 :: March 24th, 2005
* Added support for SELECT statements
v1.0 :: March 24th, 2005
* Added support for transactions
* Added basic finalizers
v0.0 :: March 24th, 2005
* Initial release
1. Locking and Concurrency in SQLite Version 3