Bigloo supports database programming. The current version proposes
a SQLite binding.
The Bigloo's C back-end supports SQL queries. It relies on the SQLite
library (
http://www.sqlite.org/). The SQLite binding is accessible
to Bigloo via the 
sqlite library. Here is an example of module
that uses this library.
| (module example1
  (library sqlite))
 (let ((db (instantiate::sqlite)))
  ...)
 | 
| 
| sqlite | bigloo sqlite class |  
The instances of the class| (class sqlite
   (path::bstring read-only (default ":memory:")))
 |  sqlitehold SQLite databases. A database
may be permanently stored on a disk or loaded in memory. The class attributepathis the location on the disk where the database is stored. The
special path:memory:denotes in-memory databases. When an instance
is created a SQLite database is opened.
 Example:
 
Binds the global variable| (define db1 (instantiate::sqlite (path "/tmp/foo.db")))
(define db2 (instantiate::sqlite))
 |  db1to a database that is stored on the file
system at location/tmp/foo.db. This example also binds the global
variabledb2to an in-memory SQLite database. | 
| 
This function closes a database previously opened by creating an instance
of the class| sqlite-close sqlite | bigloo sqlite function |  sqlite.
 Example:
 
| (let ((db (instantiate::sqlite)))
  (sqlite-exec db "CREATE TABLE table1 (x INTEGER, y INTEGER)")
  (sqlite-exec db "INSERT INTO table1 VALUES( ~a, ~a )" 1 4)
  (sqlite-close db))
 |  | 
| 
Constructs a string of characters representing an SQLite
commands. This function acts as| sqlite-format stringarg... | bigloo sqlite function |  format(see Input and Output). It is augmented with three additional escape sequence:~q,~k, and~l. The first one build a string of
characters where the characters denoting SQL strings (i.e., the
character') is automatically escaped. The escape character~kintroduces a list of SQL strings. The escape character~lintroduces a SQL list.
 Summary of all escape codes:
 Examples:~aThe corresponding value is inserted into the string 
as if printed with display.~sThe corresponding value is inserted into the string 
as if printed with write.~%A newline is inserted.~~A tilde~is inserted.~qAn SQL escaped string.~lIntroduces a list (comma separated).~kIntroduces a list of SQL strings.
 
 
 
| (module example
   (library sqlite))
 (sqlite-format "~a" "foo'bar") => "foo'bar"
(sqlite-format "~q" "foo'bar") => "'foo''bar'"
(sqlite-format "~a" '("foo'bar" "foo")) => "(foo'bar foo)"
(sqlite-format "~k" '("foo'bar" "foo")) => "'foo''bar','foo'"
(sqlite-format "~l" '("foo'bar" "foo")) => "foo'bar,foo"
 |  | 
| 
The function| sqlite-exec sqlitestringarg... | bigloo sqlite function |  sqlite-execexecutes an SQLite command. The command
is the built by implicitly invokingsqlite-formatonstringand
the optionalargarguments. This function returns a single element,
the first one returned by the SQL engine.
 Example:
 
| (module example
   (library sqlite))
 (define *db* (instantiate::sqlite))
 
 (sqlite-exec *db* "CREATE TABLE foo (x INTEGER, y INTEGER)")
(for-each (lambda (x)
		(sqlite-exec *db*  "INSERT INTO foo VALUES(~A, ~A)" x (* x x)))
	     (iota 10))
(sqlite-exec *db* "SELECT * FROM foo")
   => 9
 |  | 
| 
The function| sqlite-eval sqliteprocedurestringarg... | bigloo sqlite function |  sqlite-evalinvokes a SQLite command built by
implicitly invokingsqlite-formatonstringand the optionalargarguments. The result of the function is built by applyingprocedureto the first value returned by the SQLite call.
 Note: user callback (
 procedure) must not exit. That is they must
not invoke a function create bybind-exit. Exiting from a callback will
leave the database in a inconsistent state that prevent transactions to
be rolled back. | 
| 
Similar to| sqlite-get sqliteprocedurestringarg... | bigloo sqlite function |  sqlite-evalbut the callback is invoked with two
arguments: an array of column names and an array of values. | 
| 
The function| sqlite-for-each sqliteprocedurestringarg... | bigloo sqlite function |  sqlite-for-eachinvokes a SQLite command built by
implicitly invokingsqlite-formatonstringand the optionalargarguments. The functionprocedureis applied to all
the elements statisfying the request. It accepts two vectors. The
first one is the name of the table column, the second the values.
The functionsqlite-for-eachdoes not return any value.
 Note: user callback (
 procedure) must not exit. That is they must
not invoke a function create bybind-exit. Exiting from a callback will
leave the database in a inconsistent state that prevent transactions to
be rolled back.
 Example:
 
 
 
| (module example
   (library sqlite))
 (define *db* (instantiate::sqlite))
 
 (sqlite-exec *db* "CREATE TABLE foo (x INTEGER, y INTEGER)")
(for-each (lambda (x)
		(sqlite-exec *db*  "INSERT INTO foo VALUES(~A, ~A)" x (* x x)))
	     (iota 10))
(sqlite-map *db* 
  (lambda (keys vals) (print keys vals))
  "SELECT * FROM foo")
   -| #("x" "y") #(0 0)
            #("x" "y") #(1 1)
            ...
 |  | 
| 
The function| sqlite-map sqliteprocedurestringarg... | bigloo sqlite function |  sqlite-mapinvokes a SQLite command built by
implicitly invokingsqlite-formatonstringand the optionalargarguments. The result is a list whose elements are built by applyingprocedureto all the values returned by the SQLite call.
 Note: user callback (
 procedure) must not exit. That is they must
not invoke a function create bybind-exit. Exiting from a callback will
leave the database in a inconsistent state that prevent transactions to
be rolled back.
Example:
 
 
| (module example
   (library sqlite))
 (define *db* (instantiate::sqlite))
 
 (sqlite-exec *db* "CREATE TABLE foo (x INTEGER, y INTEGER)")
(for-each (lambda (x)
		(sqlite-exec *db*  "INSERT INTO foo VALUES(~A, ~A)" x (* x x)))
	     (iota 10))
(sqlite-map *db* 
  (lambda (s1 s2) (+ (string->integer s1) (string->integer s2))) 
  "SELECT * FROM foo")
   => (0 2 6 12 20 30 42 56 72 90)
 |  | 
Example2:
| (module example
   (library sqlite))
 (define *db* (instantiate::sqlite))
 
 (sqlite-exec *db* "CREATE TABLE foo (x INTEGER, y INTEGER)")
(for-each (lambda (x)
		(sqlite-exec *db*  "INSERT INTO foo VALUES(~A, ~A)" x (* x x)))
	     (iota 10))
(sqlite-map *db* vector "SELECT * FROM foo")
   => '(#("0" "0")
	#("1" "1")
	#("2" "4")
	#("3" "9")
	#("4" "16")
	#("5" "25")
	#("6" "36")
	#("7" "49")
	#("8" "64")
	#("9" "81"))
 | 
| 
Returns the name of tables in the database. This list can also be
obtained with| sqlite-name-of-tables sqlite | bigloo sqlite function |  
 
 
| (sqlite-map db
   (lambda (x) x)
   "SELECT name FROM sqlite_master WHERE type='table'")
 |  | 
| 
Returns the name of columns in the table.| sqlite-table-name-of-columns sqlitetable | bigloo sqlite function |  | 
| 
Returns the SQLite rowid of the last inserted row.| sqlite-last-insert-rowid sqlite | bigloo sqlite function |  |