This appendix lists the C library functions that can be used to communicate with a SQLite database.
The core interface to the SQLite library is considered to be just three functions that allow you to open and close a database and to execute a query using a user-defined callback function. In this section we'll also look at the error codes returned from the core API.
You can open and close a database as follows:
sqlite *sqlite_open( const char *dbname, int mode, char **errmsg ); void sqlite_close(sqlite *db);
The return value of sqlite_open()
and the argument to sqlite_close()
is an opaque sqlite
data structure.
typedef struct sqlite sqlite;
You can execute a query as follows:
int sqlite_exec( sqlite *db, char *sql, int (*xCallback)(void *, int, char **, char **), void pArg, char **errmsg );
The callback function has the following prototype:
int callback(void *pArg, int argc, char **argv, char **columnNames) { return 0; }
This section describes SQLite error codes; each error code is followed by a description of its meaning.
Returned if everything worked and there were no errors.
Indicates an error in the SQL statement being executed.
Indicates that an internal consistency check within the SQLite library failed. This code will only ever be returned as a result of a bug, and such occurrences should be reported to the SQLite mailing list.
Indicates that the database file cannot be opened due to insufficient file permissions.
This value is returned if the callback function returns a non-zero value.
Indicates that another program or thread has the database locked. Only one thread can open a database file for writing at a time, though multiple reads can take place simultaneously.
Indicates that the database is locked by a recursive call to sqlite_exec()
. Calls to sqlite_exec()
from within a callback function are permitted as long as they do not attempt to write to the same table.
This value is returned if a call to malloc()
fails due to not enough system memory being available.
Indicates that an attempt was made to write to a database file that was opened in read-only mode.
This value is returned if a database operation is interrupted by the sqlite_interrupt()
function being called.
This value is returned if the operating system fails to perform a disk I/O operation, for instance if there is no space left on the device.
This value would only be returned as a consequence of an unknown error in SQLite or a hardware or operating-system malfunction. It may indicate that the database file has become corrupted, or that a disk I/O error has forced SQLite to leave the database file in a corrupted state.
For internal use only. This value will never be returned by sqlite_exec()
.
This value is returned if an insert operation fails because the database is too big to take any more information, for instance when the size of the database file would exceed the operating system's file size limit.
This value indicates that that database file could not be opened for some other reason that does not have its own return code.
Indicates that the file-locking protocol on SQLite's rollback journal files has been violated.
For internal use only. This value will never be returned by sqlite_exec()
.
Indicates that another process has altered the database schema since it was first read into memory when the database was initially opened. SQLite will re-read the schema when this happens but will not be able to complete the SQL statement that was being processed. Submitting the statement a second time will usually allow the command to be executed.
Indicates that the maximum row size for a table would be exceeded. The limit is defined at compile time by the MAX_BYTES_PER_ROW
value in sqliteInt.h
.
Indicates that the SQL statement would have violated a database constraint.
This value is returned when an attempt is made to insert non-integer data into an INTEGER PRIMARY KEY
column.
Indicates that the SQLite library has been misused in some way, for instance calling sqlite_exec()
after the database has been closed, or calling sqlite_step()
after a SQLITE_DONE
or SQLITE_ERROR
return code.
This value is returned if an attempt is made to access a database file larger than 2GB in size on a legacy operating system that does not include large file support.
Indicates that the authorizer callback has disallowed the SQL you are attempting to execute.
The non-callback API provides an alternative way to retrieve data from a SQLite database by compiling an SQL statement into a virtual machine of type sqlite_vm
:
typedef struct sqlite_vm sqlite_vm;
You can create a SQLite virtual machine as follows:
int sqlite_compile( sqlite *db, /* The open database */ const char *zSql, /* SQL statement to be compiled */ const char **pzTail, /* OUT: uncompiled tail of zSql */ sqlite_vm **ppVm, /* OUT: the virtual machine to execute zSql */ char **pzErrmsg /* OUT: Error message. */ );
The return code from sqlite_compile()
is SQLITE_OK
if the operation is successful; otherwise, one of the error codes listed in the preceding example is returned.
Each invocation of sqlite_step()
for a virtual machine, except the last one, returns a single row of the result:
int sqlite_step( sqlite_vm *pVm, /* The virtual machine to execute */ int *pN, /* OUT: Number of columns in result */ const char ***pazValue, /* OUT: Column data */ const char ***pazColName /* OUT: Column names and datatypes */ ); int sqlite_finalize( sqlite_vm *pVm, /* The virtual machine to be finalized */ char **pzErrMsg /* OUT: Error message */ );
The return code from sqlite_step()
can be SQLITE_BUSY
, SQLITE_ERROR
, SQLITE_MISUSE
, or either of the following.
Indicates that another row of result data is available.
Indicates that the SQL statement has been completely executed and sqlite_finalize()
should now be called.
The return code from sqlite_finalize()
indicates the overall success of the SQL command and will be the same as if the query had been executed using sqlite_exec()
.
The extended API provides a range of non-core functions to assist with development of software that uses an embedded SQLite database.
Several functions can return information about changes that have been made to the database.
Returns the most recently assigned autoincrementing value of an INTEGER PRIMARY KEY
field.
Returns the number of rows affected by an UPDATE
or DELETE
statement.
Returns TRUE
if a complete SQL statement is provided, that is, the statement ends with a semicolon. Returns FALSE
if more characters are required.
Causes the current database operation to exist at the first opportunity, returning SQLITE_INTERRUPT
to the calling function.
The following function fetches the entire result of a database query with a single function call:
int sqlite_get_table( sqlite *db, char *sql, char ***result, int *nrow, int *ncolumn, char **errmsg );
The result
will be an array of string pointers containing one element for each column of each row in the result. The first ncolumn
elements contain the column names returned. Use nrow
and ncolumn
to determine which elements of the array correspond to which values.
The return code from sqlite_get_table()
is the same as if sqlite_exec()
had executed the query.
void sqlite_free_table(char **azResult);
Frees memory allocated by sqlite_get_table()
when it is no longer required.
Works like sprintf()
but also allows the format strings %q
and %Q
to manipulate strings for database storage. %q
escapes any single quotes by doubling the quote character; %Q
additionally encloses the result string within single quotes.
int sqlite_exec_printf( sqlite *db, char *sqlFormat, int (*)(void *, int, char **, char **), void *pArg, char **errmsg, ... );
Combines sqlite_exec()
with sqlite_mprintf()
. The format string references items from the sixth argument onwards.
int sqlite_get_table_printf( sqlite *db, char *sql, char ***result, int *nrow, int *ncolumn, char **errmsg, ... );
Combines sqlite_get_table()
with sqlite_mprintf()
. The format string references items from the seventh argument onwards.
Where the library allocates memory using malloc()
and returns a pointer to that data, such as errmsg
or the result of sqlite_mprintf()
, it is the responsibility of the calling program to free that memory.
SQLite allows you to add new functions to the SQL language that can subsequently be used in your queries.
int sqlite_create_function( sqlite *db, const char *zName, int nArg, void (*xFunc)(sqlite_func*,int,const char**), void *pUserData );
Creates a regular function in SQL from the function pointed to by xFunc
.
int sqlite_create_aggregate( sqlite *db, const char *zName, int nArg, void (*xStep)(sqlite_func*,int,const char**), void (*xFinalize)(sqlite_func*), void *pUserData );
Creates an aggregating function with function xStep
executed once for each row returned by the query, and xFinalize
invoked once after all rows have been returned.
The xFunc
and xStep
arguments are pointers to functions with the following prototype.
void xFunc( sqlite_func *context, int argc, const char **argv );
The finalize function requires only the context
argument.
void xFinalize(sqlite_func *context);
The context
argument is an opaque data type sqlite_func
.
typedef struct sqlite_func sqlite_func;
char *sqlite_set_result_string( sqlite_func *p, const char *zResult, int n ); void sqlite_set_result_int( sqlite_func *p, int iResult ); void sqlite_set_result_double( sqlite_func *p, double rResult );
Use the appropriate function for the data type that is to be returned to SQL.
Returns an error code to SQLite.
The integer n
parameter to sqlite_set_result_string()
and sqlite_set_result_error()
is the number of characters to be returned. A negative value will return up to and including the first