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. | 
| 
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 |  |