From 3a84069da31f573908bf2f12f3bddae25540e133 Mon Sep 17 00:00:00 2001
From: drh
The interface to the SQLite library consists of three core functions, one opaque data structure, and some constants used as return values. @@ -32,26 +32,28 @@ typedef struct sqlite sqlite; sqlite *sqlite_open(const char *dbname, int mode, char **errmsg); -void sqlite_close(sqlite*); +void sqlite_close(sqlite *db); int sqlite_exec( - sqlite*, + sqlite *db, char *sql, - int (*)(void*,int,char**,char**), - void*, + int (*xCallback)(void*,int,char**,char**), + void *pArg, char **errmsg );
The above is all you really need to know in order to use SQLite -in your C or C++ programs. There are other convenience functions +in your C or C++ programs. There are other interface functions available (and described below) but we will begin by describing the core functions shown above.
-Use the sqlite_open() function to open an existing SQLite
+
+ Use the sqlite_open function to open an existing SQLite
database or to create a new SQLite database. The first argument
is the database name. The second argument is intended to signal
whether the database is going to be used for reading and writing
@@ -74,28 +76,30 @@ additional temporary files may be created during the execution of
an SQL command in order to store the database rollback journal or
temporary and intermediate results of a query. The return value of the sqlite_open() function is a
+ The return value of the sqlite_open function is a
pointer to an opaque sqlite structure. This pointer will
be the first argument to all subsequent SQLite function calls that
deal with the same database. NULL is returned if the open fails
for any reason. To close an SQLite database, call the sqlite_close()
+ To close an SQLite database, call the sqlite_close
function passing it the sqlite structure pointer that was obtained
from a prior call to sqlite_open.
If a transaction is active when the database is closed, the transaction
is rolled back. The sqlite_exec() function is used to process SQL statements
+ The sqlite_exec function is used to process SQL statements
and queries. This function requires 5 parameters as follows: A pointer to the sqlite structure obtained from a prior call
- to sqlite_open().1.1 Opening a database
+
+Closing the database
+
+1.2 Closing the database
-Executing SQL statements
+
+1.3 Executing SQL statements
-
A null-terminated string containing the text of one or more SQL statements and/or queries to be processed.
A pointer to a callback function which is invoked once for each
@@ -121,8 +125,9 @@ int Callback(void *pArg, int argc, char **argv, char **columnNames){
}
+
The first argument to the callback is just a copy of the fourth argument
-to sqlite_exec() This parameter can be used to pass arbitrary
+to sqlite_exec This parameter can be used to pass arbitrary
information through to the callback function from client code.
The second argument is the number of columns in the query result.
The third argument is an array of pointers to strings where each string
@@ -137,9 +142,17 @@ argv[i][0] == 0
The names of the columns are contained in the fourth argument. If the EMPTY_RESULT_CALLBACKS pragma is set to ON and the result of
+ The names of the columns are contained in first argc
+entries of the fourth argument.
+If the SHOW_DATATYPES pragma
+is on (it is off by default) then
+the second argc entries in the 4th argument are the datatypes
+for the corresponding columns.
+ If the
+EMPTY_RESULT_CALLBACKS pragma is set to ON and the result of
a query is an empty set, then the callback is invoked once with the
third parameter (argv) set to 0. In other words
-
argv[i] == 0
@@ -152,14 +165,15 @@ columns if there had been a result.
The default behavior is not to invoke the callback at all if the
result set is empty.
The callback function should normally return 0. If the callback function returns non-zero, the query is immediately aborted and -sqlite_exec() will return SQLITE_ABORT.
+sqlite_exec will return SQLITE_ABORT. --The sqlite_exec() function normally returns SQLITE_OK. But +The sqlite_exec function normally returns SQLITE_OK. But if something goes wrong it can return a different value to indicate the type of error. Here is a complete list of the return codes:
@@ -187,6 +201,10 @@ the type of error. Here is a complete list of the return codes: #define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */ #define SQLITE_MISMATCH 20 /* Data type mismatch */ #define SQLITE_MISUSE 21 /* Library used incorrectly */ +#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ +#define SQLITE_AUTH 23 /* Authorization denied */ +#define SQLITE_ROW 100 /* sqlite_step() has another row ready */ +#define SQLITE_DONE 101 /* sqlite_step() has finished executing */@@ -202,12 +220,12 @@ The meanings of these various return values are as follows:
This value indicates that an internal consistency check within the SQLite library failed. This can only happen if there is a bug in the SQLite library. If you ever get an SQLITE_INTERNAL reply from -an sqlite_exec() call, please report the problem on the SQLite +an sqlite_exec call, please report the problem on the SQLite mailing list.
This return value indicates that there was an error in the SQL -that was passed into the sqlite_exec(). +that was passed into the sqlite_exec.
This return value says that the access permissions on the database @@ -226,21 +244,21 @@ entire database.
This return code is similar to SQLITE_BUSY in that it indicates that the database is locked. But the source of the lock is a recursive -call to sqlite_exec(). This return can only occur if you attempt -to invoke sqlite_exec() from within a callback routine of a query -from a prior invocation of sqlite_exec(). Recursive calls to -sqlite_exec() are allowed as long as they do +call to sqlite_exec. This return can only occur if you attempt +to invoke sqlite_exec from within a callback routine of a query +from a prior invocation of sqlite_exec. Recursive calls to +sqlite_exec are allowed as long as they do not attempt to write the same table.
This value is returned if a call to malloc() fails. +
This value is returned if a call to malloc fails.
This return code indicates that an attempt was made to write to a database file that is opened for reading only.
This value is returned if a call to sqlite_interrupt() +
This value is returned if a call to sqlite_interrupt interrupts a database operation in progress.
This error might occur if one or more of the SQLite API routines is used incorrectly. Examples of incorrect usage include calling -sqlite_exec() after the database has been closed using -sqlite_close() or calling sqlite_exec() with the same +sqlite_exec after the database has been closed using +sqlite_close or +calling sqlite_exec with the same database pointer simultaneously from two separate threads.
This error means that you have attempts to create or access a file +database file that is larger that 2GB on a legacy Unix machine that +lacks large file support. +
This error indicates that the authorizer callback +has disallowed the SQL you are attempting to execute. +
This is one of the return codes from the +sqlite_step routine which is part of the non-callback API. +It indicates that another row of result data is available. +
This is one of the return codes from the +sqlite_step routine which is part of the non-callback API. +It indicates that the SQL statement has been completely executed and +the sqlite_finalize routine is ready to be called. +
Only the three core routines shown above are required to use +
+The sqlite_exec routine described above used to be the only +way to retrieve data from an SQLite database. But many programmers found +it inconvenient to use a callback function to obtain results. So beginning +with SQLite version 2.7.7, a second access interface is available that +does not use callbacks. +
+ ++The new interface uses three separate functions to replace the single +sqlite_exec function. +
+ ++ ++typedef struct sqlite_vm sqlite_vm; + +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. */ +); + +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 strategy is to compile a single SQL statement using +sqlite_compile then invoke sqlite_step multiple times, +once for each row of output, and finally call sqlite_finalize +to clean up after the SQL has finished execution. +
+ ++The sqlite_compile "compiles" a single SQL statement (specified +by the second parameter) and generates a virtual machine that is able +to execute that statement. +As with must interface routines, the first parameter must be a pointer +to an sqlite structure that was obtained from a prior call to +sqlite_open. + +
+A pointer to the virtual machine is stored in a pointer which is passed +in as the 4th parameter. +Space to hold the virtual machine is dynamically allocated. To avoid +a memory leak, the calling function must invoke +sqlite_finalize on the virtual machine after it has finished +with it. +The 4th parameter may be set to NULL if an error is encountered during +compilation. +
+ ++If any errors are encountered during compilation, an error message is +written into memory obtained from malloc and the 5th parameter +is made to point to that memory. If the 5th parameter is NULL, then +no error message is generated. If the 5th parameter is not NULL, then +the calling function should dispose of the memory containing the error +message by calling sqlite_freemem. +
+ ++If the 2nd parameter actually contains two or more statements of SQL, +only the first statement is compiled. (This is different from the +behavior of sqlite_exec which executes all SQL statements +in its input string.) The 3rd parameter to sqlite_compile +is made to point to the first character beyond the end of the first +statement of SQL in the input. If the 2nd parameter contains only +a single SQL statement, then the 3rd parameter will be made to point +to the '\000' terminator at the end of the 2nd parameter. +
+ ++On success, sqlite_compile returns SQLITE_OK. +Otherwise and error code is returned. +
+ ++After a virtual machine has been generated using sqlite_compile +it is executed by one or more calls to sqlite_step. Each +invocation of sqlite_step, except the last one, +returns a single row of the result. +The number of columns in the result is stored in the integer that +the 2nd parameter points to. +The pointer specified by the 3rd parameter is made to point +to an array of pointers to column values. +The pointer in the 4th parameter is made to point to an array +of pointers to column names and datatypes. +The 2nd through 4th parameters to sqlite_step convey the +same information as the 2nd through 4th parameters of the +callback routine when using +the sqlite_exec interface. Except, with sqlite_step +the column datatype information is always included in the in the +4th parameter regardless of whether or not the +SHOW_DATATYPES pragma +is on or off. +
+ ++Each invocation of sqlite_step returns an integer code that +indicates what happened during that step. This code may be +SQLITE_BUSY, SQLITE_ROW, SQLITE_DONE, SQLITE_ERROR, or +SQLITE_MISUSE. +
+ ++If the virtual machine is unable to open the database file because +it is locked by another thread or process, sqlite_step +will return SQLITE_BUSY. The calling function should do some other +activity, or sleep, for a short amount of time to give the lock a +chance to clear, then invoke sqlite_step again. This can +be repeated as many times as desired. +
+ ++Whenever another row of result data is available, +sqlite_step will return SQLITE_ROW. The row data is +stored in an array of pointers to strings and the 2nd parameter +is made to point to this array. +
+ ++When all processing is complete, sqlite_step will return +either SQLITE_DONE or SQLITE_ERROR. SQLITE_DONE indicates that the +statement completed successfully and SQLITE_ERROR indicates that there +was a run-time error. (The details of the error are obtained from +sqlite_finalize.) It is a misuse of the library to attempt +to call sqlite_step again after it has returned SQLITE_DONE +or SQLITE_ERROR. +
+ ++When sqlite_step returns SQLITE_DONE or SQLITE_ERROR, +the *pN and *pazColName values are set to the number of columns +in the result set and to the names of the columns, just as they +are for an SQLITE_ROW return. This allows the calling code to +find the number of result columns and the column names and datatypes +even if the result set is empty. The *pazValue parameter is always +set to NULL when the return codes is SQLITE_DONE or SQLITE_ERROR. +If the SQL being executed is a statement that does not +return a result (such as an INSERT or an UPDATE) then *pN will +be set to zero and *pazColName will be set to NULL. +
+ ++If you abuse the library by trying to call sqlite_step +inappropriately it will attempt return SQLITE_MISUSE. +This can happen if you call sqlite_step() on the same virtual machine +at the same +time from two or more threads or if you call sqlite_step() +again after it returned SQLITE_DONE or SQLITE_ERROR or if you +pass in an invalid virtual machine pointer to sqlite_step(). +You should not depend on the SQLITE_MISUSE return code to indicate +an error. It is possible that a misuse of the interface will go +undetected and result in a program crash. The SQLITE_MISUSE is +intended as a debugging aid only - to help you detect incorrect +usage prior to a mishap. The misuse detection logic is not guaranteed +to work in every case. +
+ ++Every virtual machine that sqlite_compile creates should +eventually be handed to sqlite_finalize. The sqlite_finalize() +procedure deallocates the memory and other resources that the virtual +machine uses. Failure to call sqlite_finalize() will result in +resource leaks in your program. +
+ ++The sqlite_finalize routine also returns the result code +that indicates success or failure of the SQL operation that the +virtual machine carried out. +The value returned by sqlite_finalize() will be the same as would +have been returned had the same SQL been executed by sqlite_exec. +The error message returned will also be the same. +
+ ++It is acceptable to call sqlite_finalize on a virtual machine +before sqlite_step has returned SQLITE_DONE. Doing so has +the effect of interrupting the operation in progress. Partially completed +changes will be rolled back and the database will be restored to its +original state (unless an alternative recovery algorithm is selected using +an ON CONFLICT clause in the SQL being executed.) The effect is the +same as if a callback function of sqlite_exec had returned +non-zero. +
+ ++It is also acceptable to call sqlite_finalize on a virtual machine +that has never been passed to sqlite_step even once. +
+ +Only the three core routines described in section 1.0 are required to use SQLite. But there are many other functions that provide useful interfaces. These extended routines are as follows:
@@ -389,7 +640,7 @@ void sqlite_freemem(char*);All of the above definitions are included in the "sqlite.h" header file that comes in the source tree.
-Every row of an SQLite table has a unique integer key. If the table has a column labeled INTEGER PRIMARY KEY, then that column @@ -403,33 +654,33 @@ KEY column, or if the table does have an INTEGER PRIMARY KEY but the value for that column is not specified in the VALUES clause of the insert, then the key is automatically generated. You can find the value of the key for the most recent INSERT statement using the -sqlite_last_insert_rowid() API function.
+sqlite_last_insert_rowid API function. -The sqlite_changes() API function returns the number of rows +
The sqlite_changes API function returns the number of rows that were inserted, deleted, or modified during the most recent -sqlite_exec() call. The number reported includes any changes +sqlite_exec call. The number reported includes any changes that were later undone by a ROLLBACK or ABORT. But rows that are deleted because of a DROP TABLE are not counted.
SQLite implements the command "DELETE FROM table" (without a WHERE clause) by dropping the table then recreating it. This is much faster than deleting the elements of the table individually. -But it also means that the value returned from sqlite_changes() +But it also means that the value returned from sqlite_changes will be zero regardless of the number of elements that were originally in the table. If an accurate count of the number of elements deleted is necessary, use "DELETE FROM table WHERE 1" instead.
-The sqlite_get_table() function is a wrapper around -sqlite_exec() that collects all the information from successive +
The sqlite_get_table function is a wrapper around +sqlite_exec that collects all the information from successive callbacks and writes it into memory obtained from malloc(). This is a convenience function that allows the application to get the entire result of a database query with a single function call.
-The main result from sqlite_get_table() is an array of pointers +
The main result from sqlite_get_table is an array of pointers to strings. There is one element in this array for each column of each row in the result. NULL results are represented by a NULL pointer. In addition to the regular data, there is an added row at the @@ -444,7 +695,7 @@ SELECT employee_name, login, host FROM users WHERE logic LIKE 'd%';
This query will return the name, login and host computer name for every employee whose login begins with the letter "d". If this -query is submitted to sqlite_get_table() the result might +query is submitted to sqlite_get_table the result might look like this:
@@ -465,7 +716,7 @@ result[8] = "zadok" the result[] array contains a NULL pointer at that slot.-If the result set of a query is empty, then by default -sqlite_get_table() will set nrow to 0 and leave its +sqlite_get_table will set nrow to 0 and leave its result parameter is set to NULL. But if the EMPTY_RESULT_CALLBACKS pragma is ON then the result parameter is initialized to the names of the columns only. For example, consider this query which has @@ -498,47 +749,47 @@ result[1] = "login"
result[2] = "host"
Memory to hold the information returned by sqlite_get_table() +
Memory to hold the information returned by sqlite_get_table is obtained from malloc(). But the calling function should not try to free this information directly. Instead, pass the complete table -to sqlite_free_table() when the table is no longer needed. -It is safe to call sqlite_free_table() with a NULL pointer such +to sqlite_free_table when the table is no longer needed. +It is safe to call sqlite_free_table with a NULL pointer such as would be returned if the result set is empty.
-The sqlite_get_table() routine returns the same integer -result code as sqlite_exec().
+The sqlite_get_table routine returns the same integer +result code as sqlite_exec.
-The sqlite_interrupt() function can be called from a +
The sqlite_interrupt function can be called from a different thread or from a signal handler to cause the current database operation to exit at its first opportunity. When this happens, -the sqlite_exec() routine (or the equivalent) that started +the sqlite_exec routine (or the equivalent) that started the database operation will return SQLITE_INTERRUPT.
-The next interface routine to SQLite is a convenience function used to test whether or not a string forms a complete SQL statement. -If the sqlite_complete() function returns true when its input +If the sqlite_complete function returns true when its input is a string, then the argument forms a complete SQL statement. There are no guarantees that the syntax of that statement is correct, -but we at least know the statement is complete. If sqlite_complete() +but we at least know the statement is complete. If sqlite_complete returns false, then more text is required to complete the SQL statement.
-For the purpose of the sqlite_complete() function, an SQL +
For the purpose of the sqlite_complete function, an SQL statement is complete if it ends in a semicolon.
-The sqlite command-line utility uses the sqlite_complete() -function to know when it needs to call sqlite_exec(). After each -line of input is received, sqlite calls sqlite_complete() -on all input in its buffer. If sqlite_complete() returns true, -then sqlite_exec() is called and the input buffer is reset. If -sqlite_complete() returns false, then the prompt is changed to +
The sqlite command-line utility uses the sqlite_complete +function to know when it needs to call sqlite_exec. After each +line of input is received, sqlite calls sqlite_complete +on all input in its buffer. If sqlite_complete returns true, +then sqlite_exec is called and the input buffer is reset. If +sqlite_complete returns false, then the prompt is changed to the continuation prompt and another line of text is read and added to the input buffer.
-The SQLite library exports the string constant named sqlite_version which contains the version number of the @@ -548,7 +799,7 @@ the SQLITE_VERSION macro against the sqlite_version string constant to verify that the version number of the header file and the library match.
-By default, SQLite assumes that all data uses a fixed-size 8-bit character (iso8859). But if you give the --enable-utf8 option @@ -565,9 +816,9 @@ be changed at run-time. This is a compile-time option only. The sqlite_encoding character string just tells you how the library was compiled.
-The sqlite_busy_handler() procedure can be used to register +
The sqlite_busy_handler procedure can be used to register a busy callback with an open SQLite database. The busy callback will be invoked whenever SQLite tries to access a database that is locked. The callback will typically do some other useful work, or perhaps sleep, @@ -576,8 +827,8 @@ non-zero, then SQLite tries again to access the database and the cycle repeats. If the callback returns zero, then SQLite aborts the current operation and returns SQLITE_BUSY.
-The arguments to sqlite_busy_handler() are the opaque -structure returned from sqlite_open(), a pointer to the busy +
The arguments to sqlite_busy_handler are the opaque +structure returned from sqlite_open, a pointer to the busy callback function, and a generic pointer that will be passed as the first argument to the busy callback. When SQLite invokes the busy callback, it sends it three arguments: the generic pointer @@ -587,15 +838,15 @@ to access, and the number of times that the library has attempted to access the database table or index.
For the common case where we want the busy callback to sleep, -the SQLite library provides a convenience routine sqlite_busy_timeout(). -The first argument to sqlite_busy_timeout() is a pointer to +the SQLite library provides a convenience routine sqlite_busy_timeout. +The first argument to sqlite_busy_timeout is a pointer to an open SQLite database and the second argument is a number of milliseconds. -After sqlite_busy_timeout() has been executed, the SQLite library +After sqlite_busy_timeout has been executed, the SQLite library will wait for the lock to clear for at least the number of milliseconds specified before it returns SQLITE_BUSY. Specifying zero milliseconds for the timeout restores the default behavior.
-The four utility functions
@@ -608,23 +859,23 @@ the timeout restores the default behavior. -implement the same query functionality as sqlite_exec() -and sqlite_get_table(). But instead of taking a complete +
implement the same query functionality as sqlite_exec +and sqlite_get_table. But instead of taking a complete SQL statement as their second argument, the four _printf routines take a printf-style format string. The SQL statement to be executed is generated from this format string and from whatever additional arguments are attached to the end of the function call.
There are two advantages to using the SQLite printf -functions instead of sprintf(). First of all, with the +functions instead of sprintf. First of all, with the SQLite printf routines, there is never a danger of overflowing a -static buffer as there is with sprintf(). The SQLite +static buffer as there is with sprintf. The SQLite printf routines automatically allocate (and later frees) as much memory as is necessary to hold the SQL statements generated.
The second advantage the SQLite printf routines have over -sprintf() are two new formatting options specifically designed +sprintf are two new formatting options specifically designed to support string literals in SQL. Within the format string, the %q formatting option works very much like %s in that it reads a null-terminated string from the argument list and inserts @@ -717,7 +968,7 @@ by passing it to sqlite_freemem().
-Beginning with version 2.4.0, SQLite allows the SQL language to be extended with new functions implemented as C code. The following interface @@ -800,13 +1051,13 @@ new SQL functions, review the SQLite source code in the file func.c.
-If SQLite is compiled with the THREADSAFE preprocessor macro set to 1, then it is safe to use SQLite from two or more threads of the same process at the same time. But each thread should have its own sqlite* -pointer returned from sqlite_open(). It is never safe for two +pointer returned from sqlite_open. It is never safe for two or more threads to access the same sqlite* pointer at the same time.
@@ -823,7 +1074,7 @@ Under Unix, an sqlite* pointer should not be carried across a should open its own copy of the database after the fork(). -For examples of how the SQLite C/C++ interface can be used, refer to the source code for the sqlite program in the diff --git a/www/lang.tcl b/www/lang.tcl index 5cea5f381e..113c42dc25 100644 --- a/www/lang.tcl +++ b/www/lang.tcl @@ -1,7 +1,7 @@ # # Run this Tcl script to generate the sqlite.html file. # -set rcsid {$Id: lang.tcl,v 1.48 2003/01/26 15:28:18 jplyon Exp $} +set rcsid {$Id: lang.tcl,v 1.49 2003/01/29 22:58:27 drh Exp $} puts {
@@ -1103,6 +1103,7 @@ with caution. synchronous pragma does the same thing but only applies the setting to the current session. +PRAGMA empty_result_callbacks = ON;
PRAGMA empty_result_callbacks = OFF;
When on, the EMPTY_RESULT_CALLBACKS pragma causes the callback @@ -1145,14 +1146,17 @@ with caution.
a description of all problems. If everything is in order, "ok" is returned. +PRAGMA show_datatypes = ON;
PRAGMA show_datatypes = OFF;
When turned on, the SHOW_DATATYPES pragma causes extra entries containing the names of datatypes of columns to be appended to the 4th ("columnNames") argument to sqlite_exec() callbacks. When turned off, the 4th argument to callbacks contains only the column names. - SQLite datatypes are always either "TEXT" - or "NUMERIC". + The datatype for table columns is taken from the CREATE TABLE statement + that defines the table. Columns with an unspecified datatype have a + datatype of "NUMERIC" and the results of expression have a datatype of + either "TEXT" or "NUMERIC" depending on the expression. The following chart illustrates the difference for the query "SELECT 'xyzzy', 5, NULL AS empty ":