aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl
diff options
context:
space:
mode:
authordan miller2007-10-21 08:36:32 +0000
committerdan miller2007-10-21 08:36:32 +0000
commit2f8d7092bc2c9609fa98d6888106b96f38b22828 (patch)
treeda6c37579258cc965b52a75aee6135fe44237698 /libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl
parent* Committing new PolicyManager based on an ACL system. (diff)
downloadopensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.zip
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.gz
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.bz2
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.xz
libraries moved to opensim-libs, a new repository
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl1116
1 files changed, 0 insertions, 1116 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl
deleted file mode 100644
index c784ff0..0000000
--- a/libraries/sqlite/unix/sqlite-3.5.1/www/c_interface.tcl
+++ /dev/null
@@ -1,1116 +0,0 @@
1#
2# Run this Tcl script to generate the sqlite.html file.
3#
4set rcsid {$Id: c_interface.tcl,v 1.43 2004/11/19 11:59:24 danielk1977 Exp $}
5source common.tcl
6header {The C language interface to the SQLite library}
7puts {
8<h2>The C language interface to the SQLite library</h2>
9
10<p>The SQLite library is designed to be very easy to use from
11a C or C++ program. This document gives an overview of the C/C++
12programming interface.</p>
13
14<h3>1.0 The Core API</h3>
15
16<p>The interface to the SQLite library consists of three core functions,
17one opaque data structure, and some constants used as return values.
18The core interface is as follows:</p>
19
20<blockquote><pre>
21typedef struct sqlite sqlite;
22#define SQLITE_OK 0 /* Successful result */
23
24sqlite *sqlite_open(const char *dbname, int mode, char **errmsg);
25
26void sqlite_close(sqlite *db);
27
28int sqlite_exec(
29 sqlite *db,
30 char *sql,
31 int (*xCallback)(void*,int,char**,char**),
32 void *pArg,
33 char **errmsg
34);
35</pre></blockquote>
36
37<p>
38The above is all you really need to know in order to use SQLite
39in your C or C++ programs. There are other interface functions
40available (and described below) but we will begin by describing
41the core functions shown above.
42</p>
43
44<a name="sqlite_open">
45<h4>1.1 Opening a database</h4>
46
47<p>Use the <b>sqlite_open</b> function to open an existing SQLite
48database or to create a new SQLite database. The first argument
49is the database name. The second argument is intended to signal
50whether the database is going to be used for reading and writing
51or just for reading. But in the current implementation, the
52second argument to <b>sqlite_open</b> is ignored.
53The third argument is a pointer to a string pointer.
54If the third argument is not NULL and an error occurs
55while trying to open the database, then an error message will be
56written to memory obtained from malloc() and *errmsg will be made
57to point to this error message. The calling function is responsible
58for freeing the memory when it has finished with it.</p>
59
60<p>The name of an SQLite database is the name of a file that will
61contain the database. If the file does not exist, SQLite attempts
62to create and initialize it. If the file is read-only (due to
63permission bits or because it is located on read-only media like
64a CD-ROM) then SQLite opens the database for reading only. The
65entire SQL database is stored in a single file on the disk. But
66additional temporary files may be created during the execution of
67an SQL command in order to store the database rollback journal or
68temporary and intermediate results of a query.</p>
69
70<p>The return value of the <b>sqlite_open</b> function is a
71pointer to an opaque <b>sqlite</b> structure. This pointer will
72be the first argument to all subsequent SQLite function calls that
73deal with the same database. NULL is returned if the open fails
74for any reason.</p>
75
76<a name="sqlite_close">
77<h4>1.2 Closing the database</h4>
78
79<p>To close an SQLite database, call the <b>sqlite_close</b>
80function passing it the sqlite structure pointer that was obtained
81from a prior call to <b>sqlite_open</b>.
82If a transaction is active when the database is closed, the transaction
83is rolled back.</p>
84
85<a name="sqlite_exec">
86<h4>1.3 Executing SQL statements</h4>
87
88<p>The <b>sqlite_exec</b> function is used to process SQL statements
89and queries. This function requires 5 parameters as follows:</p>
90
91<ol>
92<li><p>A pointer to the sqlite structure obtained from a prior call
93 to <b>sqlite_open</b>.</p></li>
94<li><p>A null-terminated string containing the text of one or more
95 SQL statements and/or queries to be processed.</p></li>
96<li><p>A pointer to a callback function which is invoked once for each
97 row in the result of a query. This argument may be NULL, in which
98 case no callbacks will ever be invoked.</p></li>
99<li><p>A pointer that is forwarded to become the first argument
100 to the callback function.</p></li>
101<li><p>A pointer to an error string. Error messages are written to space
102 obtained from malloc() and the error string is made to point to
103 the malloced space. The calling function is responsible for freeing
104 this space when it has finished with it.
105 This argument may be NULL, in which case error messages are not
106 reported back to the calling function.</p></li>
107</ol>
108
109<p>
110The callback function is used to receive the results of a query. A
111prototype for the callback function is as follows:</p>
112
113<blockquote><pre>
114int Callback(void *pArg, int argc, char **argv, char **columnNames){
115 return 0;
116}
117</pre></blockquote>
118
119<a name="callback_row_data">
120<p>The first argument to the callback is just a copy of the fourth argument
121to <b>sqlite_exec</b> This parameter can be used to pass arbitrary
122information through to the callback function from client code.
123The second argument is the number of columns in the query result.
124The third argument is an array of pointers to strings where each string
125is a single column of the result for that record. Note that the
126callback function reports a NULL value in the database as a NULL pointer,
127which is very different from an empty string. If the i-th parameter
128is an empty string, we will get:</p>
129<blockquote><pre>
130argv[i][0] == 0
131</pre></blockquote>
132<p>But if the i-th parameter is NULL we will get:</p>
133<blockquote><pre>
134argv[i] == 0
135</pre></blockquote>
136
137<p>The names of the columns are contained in first <i>argc</i>
138entries of the fourth argument.
139If the <a href="pragma.html#pragma_show_datatypes">SHOW_DATATYPES</a> pragma
140is on (it is off by default) then
141the second <i>argc</i> entries in the 4th argument are the datatypes
142for the corresponding columns.
143</p>
144
145<p>If the <a href="pragma.html#pragma_empty_result_callbacks">
146EMPTY_RESULT_CALLBACKS</a> pragma is set to ON and the result of
147a query is an empty set, then the callback is invoked once with the
148third parameter (argv) set to 0. In other words
149<blockquote><pre>
150argv == 0
151</pre></blockquote>
152The second parameter (argc)
153and the fourth parameter (columnNames) are still valid
154and can be used to determine the number and names of the result
155columns if there had been a result.
156The default behavior is not to invoke the callback at all if the
157result set is empty.</p>
158
159<a name="callback_returns_nonzero">
160<p>The callback function should normally return 0. If the callback
161function returns non-zero, the query is immediately aborted and
162<b>sqlite_exec</b> will return SQLITE_ABORT.</p>
163
164<h4>1.4 Error Codes</h4>
165
166<p>
167The <b>sqlite_exec</b> function normally returns SQLITE_OK. But
168if something goes wrong it can return a different value to indicate
169the type of error. Here is a complete list of the return codes:
170</p>
171
172<blockquote><pre>
173#define SQLITE_OK 0 /* Successful result */
174#define SQLITE_ERROR 1 /* SQL error or missing database */
175#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
176#define SQLITE_PERM 3 /* Access permission denied */
177#define SQLITE_ABORT 4 /* Callback routine requested an abort */
178#define SQLITE_BUSY 5 /* The database file is locked */
179#define SQLITE_LOCKED 6 /* A table in the database is locked */
180#define SQLITE_NOMEM 7 /* A malloc() failed */
181#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
182#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
183#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
184#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
185#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
186#define SQLITE_FULL 13 /* Insertion failed because database is full */
187#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
188#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
189#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
190#define SQLITE_SCHEMA 17 /* The database schema changed */
191#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
192#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
193#define SQLITE_MISMATCH 20 /* Data type mismatch */
194#define SQLITE_MISUSE 21 /* Library used incorrectly */
195#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
196#define SQLITE_AUTH 23 /* Authorization denied */
197#define SQLITE_ROW 100 /* sqlite_step() has another row ready */
198#define SQLITE_DONE 101 /* sqlite_step() has finished executing */
199</pre></blockquote>
200
201<p>
202The meanings of these various return values are as follows:
203</p>
204
205<blockquote>
206<dl>
207<dt>SQLITE_OK</dt>
208<dd><p>This value is returned if everything worked and there were no errors.
209</p></dd>
210<dt>SQLITE_INTERNAL</dt>
211<dd><p>This value indicates that an internal consistency check within
212the SQLite library failed. This can only happen if there is a bug in
213the SQLite library. If you ever get an SQLITE_INTERNAL reply from
214an <b>sqlite_exec</b> call, please report the problem on the SQLite
215mailing list.
216</p></dd>
217<dt>SQLITE_ERROR</dt>
218<dd><p>This return value indicates that there was an error in the SQL
219that was passed into the <b>sqlite_exec</b>.
220</p></dd>
221<dt>SQLITE_PERM</dt>
222<dd><p>This return value says that the access permissions on the database
223file are such that the file cannot be opened.
224</p></dd>
225<dt>SQLITE_ABORT</dt>
226<dd><p>This value is returned if the callback function returns non-zero.
227</p></dd>
228<dt>SQLITE_BUSY</dt>
229<dd><p>This return code indicates that another program or thread has
230the database locked. SQLite allows two or more threads to read the
231database at the same time, but only one thread can have the database
232open for writing at the same time. Locking in SQLite is on the
233entire database.</p>
234</p></dd>
235<dt>SQLITE_LOCKED</dt>
236<dd><p>This return code is similar to SQLITE_BUSY in that it indicates
237that the database is locked. But the source of the lock is a recursive
238call to <b>sqlite_exec</b>. This return can only occur if you attempt
239to invoke sqlite_exec from within a callback routine of a query
240from a prior invocation of sqlite_exec. Recursive calls to
241sqlite_exec are allowed as long as they do
242not attempt to write the same table.
243</p></dd>
244<dt>SQLITE_NOMEM</dt>
245<dd><p>This value is returned if a call to <b>malloc</b> fails.
246</p></dd>
247<dt>SQLITE_READONLY</dt>
248<dd><p>This return code indicates that an attempt was made to write to
249a database file that is opened for reading only.
250</p></dd>
251<dt>SQLITE_INTERRUPT</dt>
252<dd><p>This value is returned if a call to <b>sqlite_interrupt</b>
253interrupts a database operation in progress.
254</p></dd>
255<dt>SQLITE_IOERR</dt>
256<dd><p>This value is returned if the operating system informs SQLite
257that it is unable to perform some disk I/O operation. This could mean
258that there is no more space left on the disk.
259</p></dd>
260<dt>SQLITE_CORRUPT</dt>
261<dd><p>This value is returned if SQLite detects that the database it is
262working on has become corrupted. Corruption might occur due to a rogue
263process writing to the database file or it might happen due to an
264perviously undetected logic error in of SQLite. This value is also
265returned if a disk I/O error occurs in such a way that SQLite is forced
266to leave the database file in a corrupted state. The latter should only
267happen due to a hardware or operating system malfunction.
268</p></dd>
269<dt>SQLITE_FULL</dt>
270<dd><p>This value is returned if an insertion failed because there is
271no space left on the disk, or the database is too big to hold any
272more information. The latter case should only occur for databases
273that are larger than 2GB in size.
274</p></dd>
275<dt>SQLITE_CANTOPEN</dt>
276<dd><p>This value is returned if the database file could not be opened
277for some reason.
278</p></dd>
279<dt>SQLITE_PROTOCOL</dt>
280<dd><p>This value is returned if some other process is messing with
281file locks and has violated the file locking protocol that SQLite uses
282on its rollback journal files.
283</p></dd>
284<dt>SQLITE_SCHEMA</dt>
285<dd><p>When the database first opened, SQLite reads the database schema
286into memory and uses that schema to parse new SQL statements. If another
287process changes the schema, the command currently being processed will
288abort because the virtual machine code generated assumed the old
289schema. This is the return code for such cases. Retrying the
290command usually will clear the problem.
291</p></dd>
292<dt>SQLITE_TOOBIG</dt>
293<dd><p>SQLite will not store more than about 1 megabyte of data in a single
294row of a single table. If you attempt to store more than 1 megabyte
295in a single row, this is the return code you get.
296</p></dd>
297<dt>SQLITE_CONSTRAINT</dt>
298<dd><p>This constant is returned if the SQL statement would have violated
299a database constraint.
300</p></dd>
301<dt>SQLITE_MISMATCH</dt>
302<dd><p>This error occurs when there is an attempt to insert non-integer
303data into a column labeled INTEGER PRIMARY KEY. For most columns, SQLite
304ignores the data type and allows any kind of data to be stored. But
305an INTEGER PRIMARY KEY column is only allowed to store integer data.
306</p></dd>
307<dt>SQLITE_MISUSE</dt>
308<dd><p>This error might occur if one or more of the SQLite API routines
309is used incorrectly. Examples of incorrect usage include calling
310<b>sqlite_exec</b> after the database has been closed using
311<b>sqlite_close</b> or
312calling <b>sqlite_exec</b> with the same
313database pointer simultaneously from two separate threads.
314</p></dd>
315<dt>SQLITE_NOLFS</dt>
316<dd><p>This error means that you have attempts to create or access a file
317database file that is larger that 2GB on a legacy Unix machine that
318lacks large file support.
319</p></dd>
320<dt>SQLITE_AUTH</dt>
321<dd><p>This error indicates that the authorizer callback
322has disallowed the SQL you are attempting to execute.
323</p></dd>
324<dt>SQLITE_ROW</dt>
325<dd><p>This is one of the return codes from the
326<b>sqlite_step</b> routine which is part of the non-callback API.
327It indicates that another row of result data is available.
328</p></dd>
329<dt>SQLITE_DONE</dt>
330<dd><p>This is one of the return codes from the
331<b>sqlite_step</b> routine which is part of the non-callback API.
332It indicates that the SQL statement has been completely executed and
333the <b>sqlite_finalize</b> routine is ready to be called.
334</p></dd>
335</dl>
336</blockquote>
337
338<h3>2.0 Accessing Data Without Using A Callback Function</h3>
339
340<p>
341The <b>sqlite_exec</b> routine described above used to be the only
342way to retrieve data from an SQLite database. But many programmers found
343it inconvenient to use a callback function to obtain results. So beginning
344with SQLite version 2.7.7, a second access interface is available that
345does not use callbacks.
346</p>
347
348<p>
349The new interface uses three separate functions to replace the single
350<b>sqlite_exec</b> function.
351</p>
352
353<blockquote><pre>
354typedef struct sqlite_vm sqlite_vm;
355
356int sqlite_compile(
357 sqlite *db, /* The open database */
358 const char *zSql, /* SQL statement to be compiled */
359 const char **pzTail, /* OUT: uncompiled tail of zSql */
360 sqlite_vm **ppVm, /* OUT: the virtual machine to execute zSql */
361 char **pzErrmsg /* OUT: Error message. */
362);
363
364int sqlite_step(
365 sqlite_vm *pVm, /* The virtual machine to execute */
366 int *pN, /* OUT: Number of columns in result */
367 const char ***pazValue, /* OUT: Column data */
368 const char ***pazColName /* OUT: Column names and datatypes */
369);
370
371int sqlite_finalize(
372 sqlite_vm *pVm, /* The virtual machine to be finalized */
373 char **pzErrMsg /* OUT: Error message */
374);
375</pre></blockquote>
376
377<p>
378The strategy is to compile a single SQL statement using
379<b>sqlite_compile</b> then invoke <b>sqlite_step</b> multiple times,
380once for each row of output, and finally call <b>sqlite_finalize</b>
381to clean up after the SQL has finished execution.
382</p>
383
384<h4>2.1 Compiling An SQL Statement Into A Virtual Machine</h4>
385
386<p>
387The <b>sqlite_compile</b> "compiles" a single SQL statement (specified
388by the second parameter) and generates a virtual machine that is able
389to execute that statement.
390As with must interface routines, the first parameter must be a pointer
391to an sqlite structure that was obtained from a prior call to
392<b>sqlite_open</b>.
393
394<p>
395A pointer to the virtual machine is stored in a pointer which is passed
396in as the 4th parameter.
397Space to hold the virtual machine is dynamically allocated. To avoid
398a memory leak, the calling function must invoke
399<b>sqlite_finalize</b> on the virtual machine after it has finished
400with it.
401The 4th parameter may be set to NULL if an error is encountered during
402compilation.
403</p>
404
405<p>
406If any errors are encountered during compilation, an error message is
407written into memory obtained from <b>malloc</b> and the 5th parameter
408is made to point to that memory. If the 5th parameter is NULL, then
409no error message is generated. If the 5th parameter is not NULL, then
410the calling function should dispose of the memory containing the error
411message by calling <b>sqlite_freemem</b>.
412</p>
413
414<p>
415If the 2nd parameter actually contains two or more statements of SQL,
416only the first statement is compiled. (This is different from the
417behavior of <b>sqlite_exec</b> which executes all SQL statements
418in its input string.) The 3rd parameter to <b>sqlite_compile</b>
419is made to point to the first character beyond the end of the first
420statement of SQL in the input. If the 2nd parameter contains only
421a single SQL statement, then the 3rd parameter will be made to point
422to the '\000' terminator at the end of the 2nd parameter.
423</p>
424
425<p>
426On success, <b>sqlite_compile</b> returns SQLITE_OK.
427Otherwise and error code is returned.
428</p>
429
430<h4>2.2 Step-By-Step Execution Of An SQL Statement</h4>
431
432<p>
433After a virtual machine has been generated using <b>sqlite_compile</b>
434it is executed by one or more calls to <b>sqlite_step</b>. Each
435invocation of <b>sqlite_step</b>, except the last one,
436returns a single row of the result.
437The number of columns in the result is stored in the integer that
438the 2nd parameter points to.
439The pointer specified by the 3rd parameter is made to point
440to an array of pointers to column values.
441The pointer in the 4th parameter is made to point to an array
442of pointers to column names and datatypes.
443The 2nd through 4th parameters to <b>sqlite_step</b> convey the
444same information as the 2nd through 4th parameters of the
445<b>callback</b> routine when using
446the <b>sqlite_exec</b> interface. Except, with <b>sqlite_step</b>
447the column datatype information is always included in the in the
4484th parameter regardless of whether or not the
449<a href="pragma.html#pragma_show_datatypes">SHOW_DATATYPES</a> pragma
450is on or off.
451</p>
452
453<p>
454Each invocation of <b>sqlite_step</b> returns an integer code that
455indicates what happened during that step. This code may be
456SQLITE_BUSY, SQLITE_ROW, SQLITE_DONE, SQLITE_ERROR, or
457SQLITE_MISUSE.
458</p>
459
460<p>
461If the virtual machine is unable to open the database file because
462it is locked by another thread or process, <b>sqlite_step</b>
463will return SQLITE_BUSY. The calling function should do some other
464activity, or sleep, for a short amount of time to give the lock a
465chance to clear, then invoke <b>sqlite_step</b> again. This can
466be repeated as many times as desired.
467</p>
468
469<p>
470Whenever another row of result data is available,
471<b>sqlite_step</b> will return SQLITE_ROW. The row data is
472stored in an array of pointers to strings and the 2nd parameter
473is made to point to this array.
474</p>
475
476<p>
477When all processing is complete, <b>sqlite_step</b> will return
478either SQLITE_DONE or SQLITE_ERROR. SQLITE_DONE indicates that the
479statement completed successfully and SQLITE_ERROR indicates that there
480was a run-time error. (The details of the error are obtained from
481<b>sqlite_finalize</b>.) It is a misuse of the library to attempt
482to call <b>sqlite_step</b> again after it has returned SQLITE_DONE
483or SQLITE_ERROR.
484</p>
485
486<p>
487When <b>sqlite_step</b> returns SQLITE_DONE or SQLITE_ERROR,
488the *pN and *pazColName values are set to the number of columns
489in the result set and to the names of the columns, just as they
490are for an SQLITE_ROW return. This allows the calling code to
491find the number of result columns and the column names and datatypes
492even if the result set is empty. The *pazValue parameter is always
493set to NULL when the return codes is SQLITE_DONE or SQLITE_ERROR.
494If the SQL being executed is a statement that does not
495return a result (such as an INSERT or an UPDATE) then *pN will
496be set to zero and *pazColName will be set to NULL.
497</p>
498
499<p>
500If you abuse the library by trying to call <b>sqlite_step</b>
501inappropriately it will attempt return SQLITE_MISUSE.
502This can happen if you call sqlite_step() on the same virtual machine
503at the same
504time from two or more threads or if you call sqlite_step()
505again after it returned SQLITE_DONE or SQLITE_ERROR or if you
506pass in an invalid virtual machine pointer to sqlite_step().
507You should not depend on the SQLITE_MISUSE return code to indicate
508an error. It is possible that a misuse of the interface will go
509undetected and result in a program crash. The SQLITE_MISUSE is
510intended as a debugging aid only - to help you detect incorrect
511usage prior to a mishap. The misuse detection logic is not guaranteed
512to work in every case.
513</p>
514
515<h4>2.3 Deleting A Virtual Machine</h4>
516
517<p>
518Every virtual machine that <b>sqlite_compile</b> creates should
519eventually be handed to <b>sqlite_finalize</b>. The sqlite_finalize()
520procedure deallocates the memory and other resources that the virtual
521machine uses. Failure to call sqlite_finalize() will result in
522resource leaks in your program.
523</p>
524
525<p>
526The <b>sqlite_finalize</b> routine also returns the result code
527that indicates success or failure of the SQL operation that the
528virtual machine carried out.
529The value returned by sqlite_finalize() will be the same as would
530have been returned had the same SQL been executed by <b>sqlite_exec</b>.
531The error message returned will also be the same.
532</p>
533
534<p>
535It is acceptable to call <b>sqlite_finalize</b> on a virtual machine
536before <b>sqlite_step</b> has returned SQLITE_DONE. Doing so has
537the effect of interrupting the operation in progress. Partially completed
538changes will be rolled back and the database will be restored to its
539original state (unless an alternative recovery algorithm is selected using
540an ON CONFLICT clause in the SQL being executed.) The effect is the
541same as if a callback function of <b>sqlite_exec</b> had returned
542non-zero.
543</p>
544
545<p>
546It is also acceptable to call <b>sqlite_finalize</b> on a virtual machine
547that has never been passed to <b>sqlite_step</b> even once.
548</p>
549
550<h3>3.0 The Extended API</h3>
551
552<p>Only the three core routines described in section 1.0 are required to use
553SQLite. But there are many other functions that provide
554useful interfaces. These extended routines are as follows:
555</p>
556
557<blockquote><pre>
558int sqlite_last_insert_rowid(sqlite*);
559
560int sqlite_changes(sqlite*);
561
562int sqlite_get_table(
563 sqlite*,
564 char *sql,
565 char ***result,
566 int *nrow,
567 int *ncolumn,
568 char **errmsg
569);
570
571void sqlite_free_table(char**);
572
573void sqlite_interrupt(sqlite*);
574
575int sqlite_complete(const char *sql);
576
577void sqlite_busy_handler(sqlite*, int (*)(void*,const char*,int), void*);
578
579void sqlite_busy_timeout(sqlite*, int ms);
580
581const char sqlite_version[];
582
583const char sqlite_encoding[];
584
585int sqlite_exec_printf(
586 sqlite*,
587 char *sql,
588 int (*)(void*,int,char**,char**),
589 void*,
590 char **errmsg,
591 ...
592);
593
594int sqlite_exec_vprintf(
595 sqlite*,
596 char *sql,
597 int (*)(void*,int,char**,char**),
598 void*,
599 char **errmsg,
600 va_list
601);
602
603int sqlite_get_table_printf(
604 sqlite*,
605 char *sql,
606 char ***result,
607 int *nrow,
608 int *ncolumn,
609 char **errmsg,
610 ...
611);
612
613int sqlite_get_table_vprintf(
614 sqlite*,
615 char *sql,
616 char ***result,
617 int *nrow,
618 int *ncolumn,
619 char **errmsg,
620 va_list
621);
622
623char *sqlite_mprintf(const char *zFormat, ...);
624
625char *sqlite_vmprintf(const char *zFormat, va_list);
626
627void sqlite_freemem(char*);
628
629void sqlite_progress_handler(sqlite*, int, int (*)(void*), void*);
630
631</pre></blockquote>
632
633<p>All of the above definitions are included in the "sqlite.h"
634header file that comes in the source tree.</p>
635
636<h4>3.1 The ROWID of the most recent insert</h4>
637
638<p>Every row of an SQLite table has a unique integer key. If the
639table has a column labeled INTEGER PRIMARY KEY, then that column
640serves as the key. If there is no INTEGER PRIMARY KEY column then
641the key is a unique integer. The key for a row can be accessed in
642a SELECT statement or used in a WHERE or ORDER BY clause using any
643of the names "ROWID", "OID", or "_ROWID_".</p>
644
645<p>When you do an insert into a table that does not have an INTEGER PRIMARY
646KEY column, or if the table does have an INTEGER PRIMARY KEY but the value
647for that column is not specified in the VALUES clause of the insert, then
648the key is automatically generated. You can find the value of the key
649for the most recent INSERT statement using the
650<b>sqlite_last_insert_rowid</b> API function.</p>
651
652<h4>3.2 The number of rows that changed</h4>
653
654<p>The <b>sqlite_changes</b> API function returns the number of rows
655that have been inserted, deleted, or modified since the database was
656last quiescent. A "quiescent" database is one in which there are
657no outstanding calls to <b>sqlite_exec</b> and no VMs created by
658<b>sqlite_compile</b> that have not been finalized by <b>sqlite_finalize</b>.
659In common usage, <b>sqlite_changes</b> returns the number
660of rows inserted, deleted, or modified by the most recent <b>sqlite_exec</b>
661call or since the most recent <b>sqlite_compile</b>. But if you have
662nested calls to <b>sqlite_exec</b> (that is, if the callback routine
663of one <b>sqlite_exec</b> invokes another <b>sqlite_exec</b>) or if
664you invoke <b>sqlite_compile</b> to create a new VM while there is
665still another VM in existance, then
666the meaning of the number returned by <b>sqlite_changes</b> is more
667complex.
668The number reported includes any changes
669that were later undone by a ROLLBACK or ABORT. But rows that are
670deleted because of a DROP TABLE are <em>not</em> counted.</p>
671
672<p>SQLite implements the command "<b>DELETE FROM table</b>" (without
673a WHERE clause) by dropping the table then recreating it.
674This is much faster than deleting the elements of the table individually.
675But it also means that the value returned from <b>sqlite_changes</b>
676will be zero regardless of the number of elements that were originally
677in the table. If an accurate count of the number of elements deleted
678is necessary, use "<b>DELETE FROM table WHERE 1</b>" instead.</p>
679
680<h4>3.3 Querying into memory obtained from malloc()</h4>
681
682<p>The <b>sqlite_get_table</b> function is a wrapper around
683<b>sqlite_exec</b> that collects all the information from successive
684callbacks and writes it into memory obtained from malloc(). This
685is a convenience function that allows the application to get the
686entire result of a database query with a single function call.</p>
687
688<p>The main result from <b>sqlite_get_table</b> is an array of pointers
689to strings. There is one element in this array for each column of
690each row in the result. NULL results are represented by a NULL
691pointer. In addition to the regular data, there is an added row at the
692beginning of the array that contains the name of each column of the
693result.</p>
694
695<p>As an example, consider the following query:</p>
696
697<blockquote>
698SELECT employee_name, login, host FROM users WHERE login LIKE 'd%';
699</blockquote>
700
701<p>This query will return the name, login and host computer name
702for every employee whose login begins with the letter "d". If this
703query is submitted to <b>sqlite_get_table</b> the result might
704look like this:</p>
705
706<blockquote>
707nrow = 2<br>
708ncolumn = 3<br>
709result[0] = "employee_name"<br>
710result[1] = "login"<br>
711result[2] = "host"<br>
712result[3] = "dummy"<br>
713result[4] = "No such user"<br>
714result[5] = 0<br>
715result[6] = "D. Richard Hipp"<br>
716result[7] = "drh"<br>
717result[8] = "zadok"
718</blockquote>
719
720<p>Notice that the "host" value for the "dummy" record is NULL so
721the result[] array contains a NULL pointer at that slot.</p>
722
723<p>If the result set of a query is empty, then by default
724<b>sqlite_get_table</b> will set nrow to 0 and leave its
725result parameter is set to NULL. But if the EMPTY_RESULT_CALLBACKS
726pragma is ON then the result parameter is initialized to the names
727of the columns only. For example, consider this query which has
728an empty result set:</p>
729
730<blockquote>
731SELECT employee_name, login, host FROM users WHERE employee_name IS NULL;
732</blockquote>
733
734<p>
735The default behavior gives this results:
736</p>
737
738<blockquote>
739nrow = 0<br>
740ncolumn = 0<br>
741result = 0<br>
742</blockquote>
743
744<p>
745But if the EMPTY_RESULT_CALLBACKS pragma is ON, then the following
746is returned:
747</p>
748
749<blockquote>
750nrow = 0<br>
751ncolumn = 3<br>
752result[0] = "employee_name"<br>
753result[1] = "login"<br>
754result[2] = "host"<br>
755</blockquote>
756
757<p>Memory to hold the information returned by <b>sqlite_get_table</b>
758is obtained from malloc(). But the calling function should not try
759to free this information directly. Instead, pass the complete table
760to <b>sqlite_free_table</b> when the table is no longer needed.
761It is safe to call <b>sqlite_free_table</b> with a NULL pointer such
762as would be returned if the result set is empty.</p>
763
764<p>The <b>sqlite_get_table</b> routine returns the same integer
765result code as <b>sqlite_exec</b>.</p>
766
767<h4>3.4 Interrupting an SQLite operation</h4>
768
769<p>The <b>sqlite_interrupt</b> function can be called from a
770different thread or from a signal handler to cause the current database
771operation to exit at its first opportunity. When this happens,
772the <b>sqlite_exec</b> routine (or the equivalent) that started
773the database operation will return SQLITE_INTERRUPT.</p>
774
775<h4>3.5 Testing for a complete SQL statement</h4>
776
777<p>The next interface routine to SQLite is a convenience function used
778to test whether or not a string forms a complete SQL statement.
779If the <b>sqlite_complete</b> function returns true when its input
780is a string, then the argument forms a complete SQL statement.
781There are no guarantees that the syntax of that statement is correct,
782but we at least know the statement is complete. If <b>sqlite_complete</b>
783returns false, then more text is required to complete the SQL statement.</p>
784
785<p>For the purpose of the <b>sqlite_complete</b> function, an SQL
786statement is complete if it ends in a semicolon.</p>
787
788<p>The <b>sqlite</b> command-line utility uses the <b>sqlite_complete</b>
789function to know when it needs to call <b>sqlite_exec</b>. After each
790line of input is received, <b>sqlite</b> calls <b>sqlite_complete</b>
791on all input in its buffer. If <b>sqlite_complete</b> returns true,
792then <b>sqlite_exec</b> is called and the input buffer is reset. If
793<b>sqlite_complete</b> returns false, then the prompt is changed to
794the continuation prompt and another line of text is read and added to
795the input buffer.</p>
796
797<h4>3.6 Library version string</h4>
798
799<p>The SQLite library exports the string constant named
800<b>sqlite_version</b> which contains the version number of the
801library. The header file contains a macro SQLITE_VERSION
802with the same information. If desired, a program can compare
803the SQLITE_VERSION macro against the <b>sqlite_version</b>
804string constant to verify that the version number of the
805header file and the library match.</p>
806
807<h4>3.7 Library character encoding</h4>
808
809<p>By default, SQLite assumes that all data uses a fixed-size
8108-bit character (iso8859). But if you give the --enable-utf8 option
811to the configure script, then the library assumes UTF-8 variable
812sized characters. This makes a difference for the LIKE and GLOB
813operators and the LENGTH() and SUBSTR() functions. The static
814string <b>sqlite_encoding</b> will be set to either "UTF-8" or
815"iso8859" to indicate how the library was compiled. In addition,
816the <b>sqlite.h</b> header file will define one of the
817macros <b>SQLITE_UTF8</b> or <b>SQLITE_ISO8859</b>, as appropriate.</p>
818
819<p>Note that the character encoding mechanism used by SQLite cannot
820be changed at run-time. This is a compile-time option only. The
821<b>sqlite_encoding</b> character string just tells you how the library
822was compiled.</p>
823
824<h4>3.8 Changing the library's response to locked files</h4>
825
826<p>The <b>sqlite_busy_handler</b> procedure can be used to register
827a busy callback with an open SQLite database. The busy callback will
828be invoked whenever SQLite tries to access a database that is locked.
829The callback will typically do some other useful work, or perhaps sleep,
830in order to give the lock a chance to clear. If the callback returns
831non-zero, then SQLite tries again to access the database and the cycle
832repeats. If the callback returns zero, then SQLite aborts the current
833operation and returns SQLITE_BUSY.</p>
834
835<p>The arguments to <b>sqlite_busy_handler</b> are the opaque
836structure returned from <b>sqlite_open</b>, a pointer to the busy
837callback function, and a generic pointer that will be passed as
838the first argument to the busy callback. When SQLite invokes the
839busy callback, it sends it three arguments: the generic pointer
840that was passed in as the third argument to <b>sqlite_busy_handler</b>,
841the name of the database table or index that the library is trying
842to access, and the number of times that the library has attempted to
843access the database table or index.</p>
844
845<p>For the common case where we want the busy callback to sleep,
846the SQLite library provides a convenience routine <b>sqlite_busy_timeout</b>.
847The first argument to <b>sqlite_busy_timeout</b> is a pointer to
848an open SQLite database and the second argument is a number of milliseconds.
849After <b>sqlite_busy_timeout</b> has been executed, the SQLite library
850will wait for the lock to clear for at least the number of milliseconds
851specified before it returns SQLITE_BUSY. Specifying zero milliseconds for
852the timeout restores the default behavior.</p>
853
854<h4>3.9 Using the <tt>_printf()</tt> wrapper functions</h4>
855
856<p>The four utility functions</p>
857
858<p>
859<ul>
860<li><b>sqlite_exec_printf()</b></li>
861<li><b>sqlite_exec_vprintf()</b></li>
862<li><b>sqlite_get_table_printf()</b></li>
863<li><b>sqlite_get_table_vprintf()</b></li>
864</ul>
865</p>
866
867<p>implement the same query functionality as <b>sqlite_exec</b>
868and <b>sqlite_get_table</b>. But instead of taking a complete
869SQL statement as their second argument, the four <b>_printf</b>
870routines take a printf-style format string. The SQL statement to
871be executed is generated from this format string and from whatever
872additional arguments are attached to the end of the function call.</p>
873
874<p>There are two advantages to using the SQLite printf
875functions instead of <b>sprintf</b>. First of all, with the
876SQLite printf routines, there is never a danger of overflowing a
877static buffer as there is with <b>sprintf</b>. The SQLite
878printf routines automatically allocate (and later frees)
879as much memory as is
880necessary to hold the SQL statements generated.</p>
881
882<p>The second advantage the SQLite printf routines have over
883<b>sprintf</b> are two new formatting options specifically designed
884to support string literals in SQL. Within the format string,
885the %q formatting option works very much like %s in that it
886reads a null-terminated string from the argument list and inserts
887it into the result. But %q translates the inserted string by
888making two copies of every single-quote (') character in the
889substituted string. This has the effect of escaping the end-of-string
890meaning of single-quote within a string literal. The %Q formatting
891option works similar; it translates the single-quotes like %q and
892additionally encloses the resulting string in single-quotes.
893If the argument for the %Q formatting options is a NULL pointer,
894the resulting string is NULL without single quotes.
895</p>
896
897<p>Consider an example. Suppose you are trying to insert a string
898value into a database table where the string value was obtained from
899user input. Suppose the string to be inserted is stored in a variable
900named zString. The code to do the insertion might look like this:</p>
901
902<blockquote><pre>
903sqlite_exec_printf(db,
904 "INSERT INTO table1 VALUES('%s')",
905 0, 0, 0, zString);
906</pre></blockquote>
907
908<p>If the zString variable holds text like "Hello", then this statement
909will work just fine. But suppose the user enters a string like
910"Hi y'all!". The SQL statement generated reads as follows:
911
912<blockquote><pre>
913INSERT INTO table1 VALUES('Hi y'all')
914</pre></blockquote>
915
916<p>This is not valid SQL because of the apostrophy in the word "y'all".
917But if the %q formatting option is used instead of %s, like this:</p>
918
919<blockquote><pre>
920sqlite_exec_printf(db,
921 "INSERT INTO table1 VALUES('%q')",
922 0, 0, 0, zString);
923</pre></blockquote>
924
925<p>Then the generated SQL will look like the following:</p>
926
927<blockquote><pre>
928INSERT INTO table1 VALUES('Hi y''all')
929</pre></blockquote>
930
931<p>Here the apostrophy has been escaped and the SQL statement is well-formed.
932When generating SQL on-the-fly from data that might contain a
933single-quote character ('), it is always a good idea to use the
934SQLite printf routines and the %q formatting option instead of <b>sprintf</b>.
935</p>
936
937<p>If the %Q formatting option is used instead of %q, like this:</p>
938
939<blockquote><pre>
940sqlite_exec_printf(db,
941 "INSERT INTO table1 VALUES(%Q)",
942 0, 0, 0, zString);
943</pre></blockquote>
944
945<p>Then the generated SQL will look like the following:</p>
946
947<blockquote><pre>
948INSERT INTO table1 VALUES('Hi y''all')
949</pre></blockquote>
950
951<p>If the value of the zString variable is NULL, the generated SQL
952will look like the following:</p>
953
954<blockquote><pre>
955INSERT INTO table1 VALUES(NULL)
956</pre></blockquote>
957
958<p>All of the _printf() routines above are built around the following
959two functions:</p>
960
961<blockquote><pre>
962char *sqlite_mprintf(const char *zFormat, ...);
963char *sqlite_vmprintf(const char *zFormat, va_list);
964</pre></blockquote>
965
966<p>The <b>sqlite_mprintf()</b> routine works like the the standard library
967<b>sprintf()</b> except that it writes its results into memory obtained
968from malloc() and returns a pointer to the malloced buffer.
969<b>sqlite_mprintf()</b> also understands the %q and %Q extensions described
970above. The <b>sqlite_vmprintf()</b> is a varargs version of the same
971routine. The string pointer that these routines return should be freed
972by passing it to <b>sqlite_freemem()</b>.
973</p>
974
975<h4>3.10 Performing background jobs during large queries</h3>
976
977<p>The <b>sqlite_progress_handler()</b> routine can be used to register a
978callback routine with an SQLite database to be invoked periodically during long
979running calls to <b>sqlite_exec()</b>, <b>sqlite_step()</b> and the various
980wrapper functions.
981</p>
982
983<p>The callback is invoked every N virtual machine operations, where N is
984supplied as the second argument to <b>sqlite_progress_handler()</b>. The third
985and fourth arguments to <b>sqlite_progress_handler()</b> are a pointer to the
986routine to be invoked and a void pointer to be passed as the first argument to
987it.
988</p>
989
990<p>The time taken to execute each virtual machine operation can vary based on
991many factors. A typical value for a 1 GHz PC is between half and three million
992per second but may be much higher or lower, depending on the query. As such it
993is difficult to schedule background operations based on virtual machine
994operations. Instead, it is recommended that a callback be scheduled relatively
995frequently (say every 1000 instructions) and external timer routines used to
996determine whether or not background jobs need to be run.
997</p>
998
999<a name="cfunc">
1000<h3>4.0 Adding New SQL Functions</h3>
1001
1002<p>Beginning with version 2.4.0, SQLite allows the SQL language to be
1003extended with new functions implemented as C code. The following interface
1004is used:
1005</p>
1006
1007<blockquote><pre>
1008typedef struct sqlite_func sqlite_func;
1009
1010int sqlite_create_function(
1011 sqlite *db,
1012 const char *zName,
1013 int nArg,
1014 void (*xFunc)(sqlite_func*,int,const char**),
1015 void *pUserData
1016);
1017int sqlite_create_aggregate(
1018 sqlite *db,
1019 const char *zName,
1020 int nArg,
1021 void (*xStep)(sqlite_func*,int,const char**),
1022 void (*xFinalize)(sqlite_func*),
1023 void *pUserData
1024);
1025
1026char *sqlite_set_result_string(sqlite_func*,const char*,int);
1027void sqlite_set_result_int(sqlite_func*,int);
1028void sqlite_set_result_double(sqlite_func*,double);
1029void sqlite_set_result_error(sqlite_func*,const char*,int);
1030
1031void *sqlite_user_data(sqlite_func*);
1032void *sqlite_aggregate_context(sqlite_func*, int nBytes);
1033int sqlite_aggregate_count(sqlite_func*);
1034</pre></blockquote>
1035
1036<p>
1037The <b>sqlite_create_function()</b> interface is used to create
1038regular functions and <b>sqlite_create_aggregate()</b> is used to
1039create new aggregate functions. In both cases, the <b>db</b>
1040parameter is an open SQLite database on which the functions should
1041be registered, <b>zName</b> is the name of the new function,
1042<b>nArg</b> is the number of arguments, and <b>pUserData</b> is
1043a pointer which is passed through unchanged to the C implementation
1044of the function. Both routines return 0 on success and non-zero
1045if there are any errors.
1046</p>
1047
1048<p>
1049The length of a function name may not exceed 255 characters.
1050Any attempt to create a function whose name exceeds 255 characters
1051in length will result in an error.
1052</p>
1053
1054<p>
1055For regular functions, the <b>xFunc</b> callback is invoked once
1056for each function call. The implementation of xFunc should call
1057one of the <b>sqlite_set_result_...</b> interfaces to return its
1058result. The <b>sqlite_user_data()</b> routine can be used to
1059retrieve the <b>pUserData</b> pointer that was passed in when the
1060function was registered.
1061</p>
1062
1063<p>
1064For aggregate functions, the <b>xStep</b> callback is invoked once
1065for each row in the result and then <b>xFinalize</b> is invoked at the
1066end to compute a final answer. The xStep routine can use the
1067<b>sqlite_aggregate_context()</b> interface to allocate memory that
1068will be unique to that particular instance of the SQL function.
1069This memory will be automatically deleted after xFinalize is called.
1070The <b>sqlite_aggregate_count()</b> routine can be used to find out
1071how many rows of data were passed to the aggregate. The xFinalize
1072callback should invoke one of the <b>sqlite_set_result_...</b>
1073interfaces to set the final result of the aggregate.
1074</p>
1075
1076<p>
1077SQLite now implements all of its built-in functions using this
1078interface. For additional information and examples on how to create
1079new SQL functions, review the SQLite source code in the file
1080<b>func.c</b>.
1081</p>
1082
1083<h3>5.0 Multi-Threading And SQLite</h3>
1084
1085<p>
1086If SQLite is compiled with the THREADSAFE preprocessor macro set to 1,
1087then it is safe to use SQLite from two or more threads of the same process
1088at the same time. But each thread should have its own <b>sqlite*</b>
1089pointer returned from <b>sqlite_open</b>. It is never safe for two
1090or more threads to access the same <b>sqlite*</b> pointer at the same time.
1091</p>
1092
1093<p>
1094In precompiled SQLite libraries available on the website, the Unix
1095versions are compiled with THREADSAFE turned off but the windows
1096versions are compiled with THREADSAFE turned on. If you need something
1097different that this you will have to recompile.
1098</p>
1099
1100<p>
1101Under Unix, an <b>sqlite*</b> pointer should not be carried across a
1102<b>fork()</b> system call into the child process. The child process
1103should open its own copy of the database after the <b>fork()</b>.
1104</p>
1105
1106<h3>6.0 Usage Examples</h3>
1107
1108<p>For examples of how the SQLite C/C++ interface can be used,
1109refer to the source code for the <b>sqlite</b> program in the
1110file <b>src/shell.c</b> of the source tree.
1111Additional information about sqlite is available at
1112<a href="sqlite.html">sqlite.html</a>.
1113See also the sources to the Tcl interface for SQLite in
1114the source file <b>src/tclsqlite.c</b>.</p>
1115}
1116footer $rcsid