Module Sqlite3


module Sqlite3: sig .. end
API to work with Sqlite 3.* databases


Exceptions


exception Sqlite3_error of string
Sqlite_error is raised when some SQL operation is called on a nonexistent handle and the functions does not return a return code. Functions returning return code communicate erros by returning the specific code.
exception Sqlite3_range_error of (int * int)
Range error is raised if some column or bind operation refers to a nonexistent column or binding. The first entry of the returned tuple is the specified index the second is the limit which was violated.

Datas


type db 
Database handle. Used to store information regarding an open database and error code from the last operation if the function implementing that operation takes database handle as a parameter.
type stmt 
Compiled statement handle. Stores information about a compiled statement created by prepare or prepare_tail functions.

type rc =
| RC_ok
| RC_error
| RC_internal
| RC_perm
| RC_abort
| RC_busy
| RC_locked
| RC_nomem
| RC_readonly
| RC_interrupt
| RC_ioerr
| RC_corrupt
| RC_notfound
| RC_full
| RC_cantopen
| RC_protocol
| RC_empty
| RC_schema
| RC_toobig
| RC_constraint
| RC_mismatch
| RC_misuse
| RC_nofls
| RC_auth
| RC_format
| RC_range
| RC_notadb
| RC_row
| RC_done
Possible error codes from failed or successful operations.

type data =
| Data_none
| Data_null
| Data_int of int64
| Data_float of float
| Data_text of string
| Data_blob of string
Data returned by a column query.
type row = data array 
type header = string array 

General database operations


val db_open : string -> db
db_open filename opens the database file filename and returns a database handle.
val db_close : db -> unit
db_close db Closes a database file and invalidates the handle. Raises an Sqlite_error exception if invalid handle is provided.
val errcode : db -> rc
errcode db Returns the error code of the last operation. Raises an Sqlite_error exception if invalid handle is passed.
val errmsg : db -> string
errmsg db Returns the error message of the last operation. Raises an Sqlite_error exception if invalid handle is passed.
val last_insert_rowid : db -> int64
Get the index of the row inserted by the last operation.
val exec : db ->
string -> (string option array -> header -> unit) -> rc
exec sql callback performs an SQL operation. If the operation contains query statements then the callback function is called for each matching rows. The first parameter is the array of the data in string format. The second paramater is the header of the columns: an array of string of the same size as the first parameter.

Fine grained query operations


val prepare : db -> string -> stmt
Compile an SQL statement into bytecode. The statement may be only partially compiled. In this case prepare_tail can be called on the returned statement to compiled the remaining part of the SQL statement.
val prepare_tail : stmt -> stmt option
Compile an SQL statement into bytecode. Compile the remaining part of of an SQL statement.
val recompile : stmt -> unit
Recompile an SQL statement into bytecode. The statement may be only partially compiled. In this case prepare_tail can be called on the returned statement to compiled the remaining part of the SQL statement. Call this function if the statement expires due to some schema change.
val step : stmt -> rc
Perform one step of the query.
val finalize : stmt -> rc
Finalize the statement. After finalization, the only valid usage of the statement is to clall it in prepare_tail.
val reset : stmt -> rc
Reset the statement (i.e. start the query from a new, perhaps with different bindings.)
val expired : stmt -> bool
Returns true if the statement has expired. In this case. it may need to be recompiled.

Data query


val data_count : stmt -> int
Get the number of columns in the result of the last step.
val column : stmt -> int -> data
Get the data in the specified column of the result of the last step.
val column_name : stmt -> int -> string
Get the header of the specified column of the result of the last step.
val column_decltype : stmt -> int -> string
Get the declared type of the specified column of the result of the last step.

Binding data to the query


val bind : stmt -> int -> data -> rc
bind stmt index data Binds data to the free variable at the position index of statement stmt. Caution: the first variable is has index 1!
val bind_parameter_count : stmt -> int
Get the number of variable to be bound.
val bind_parameter_name : stmt -> int -> string option
Get the parameter name of the free variable at the specified position of the statement.
val bind_parameter_index : stmt -> string -> int
Lookup the position of by the name of the free variable in the statement.
val transfer_bindings : stmt -> stmt -> rc
Transfer the bindings of the statement to the other statement (second parameter)

Pretty printing functions


val string_of_data : data -> string
Returns a string representing the data.
val string_of_rc : rc -> string
Returns a string representing the return code.

Stepwise query convenience functions


val exec_sql : (stmt -> unit) -> db -> string -> unit
Perform a query in a stepwise manner.
val row_data : stmt -> data array
Get all the declared types of the columns of the last query step in an array
val row_names : stmt -> string array
Get all the column names of the last query step in an array.
val row_decltypes : stmt -> string array
Get all the data of the last query step in an array.