aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl516
1 files changed, 516 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl
new file mode 100644
index 0000000..149cf7f
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/www/capi3.tcl
@@ -0,0 +1,516 @@
1set rcsid {$Id: capi3.tcl,v 1.10 2007/04/27 17:16:22 drh Exp $}
2source common.tcl
3header {C/C++ Interface For SQLite Version 3}
4
5proc AddHyperlinks {txt} {
6 regsub -all {([^:alnum:>])(sqlite3_\w+)(\([^\)]*\))} $txt \
7 {\1<a href="capi3ref.html#\2">\2</a>\3} t2
8 puts $t2
9}
10
11AddHyperlinks {
12<h2>C/C++ Interface For SQLite Version 3</h2>
13
14<h3>1.0 Overview</h3>
15
16<p>
17SQLite version 3.0 is a new version of SQLite, derived from
18the SQLite 2.8.13 code base, but with an incompatible file format
19and API.
20SQLite version 3.0 was created to answer demand for the following features:
21</p>
22
23<ul>
24<li>Support for UTF-16.</li>
25<li>User-definable text collating sequences.</li>
26<li>The ability to store BLOBs in indexed columns.</li>
27</ul>
28
29<p>
30It was necessary to move to version 3.0 to implement these features because
31each requires incompatible changes to the database file format. Other
32incompatible changes, such as a cleanup of the API, were introduced at the
33same time under the theory that it is best to get your incompatible changes
34out of the way all at once.
35</p>
36
37<p>
38The API for version 3.0 is similar to the version 2.X API,
39but with some important changes. Most noticeably, the "<tt>sqlite_</tt>"
40prefix that occurs on the beginning of all API functions and data
41structures are changed to "<tt>sqlite3_</tt>".
42This avoids confusion between the two APIs and allows linking against both
43SQLite 2.X and SQLite 3.0 at the same time.
44</p>
45
46<p>
47There is no agreement on what the C datatype for a UTF-16
48string should be. Therefore, SQLite uses a generic type of void*
49to refer to UTF-16 strings. Client software can cast the void*
50to whatever datatype is appropriate for their system.
51</p>
52
53<h3>2.0 C/C++ Interface</h3>
54
55<p>
56The API for SQLite 3.0 includes 83 separate functions in addition
57to several data structures and #defines. (A complete
58<a href="capi3ref.html">API reference</a> is provided as a separate document.)
59Fortunately, the interface is not nearly as complex as its size implies.
60Simple programs can still make do with only 3 functions:
61<a href="capi3ref.html#sqlite3_open">sqlite3_open()</a>,
62<a href="capi3ref.html#sqlite3_exec">sqlite3_exec()</a>, and
63<a href="capi3ref.html#sqlite3_close">sqlite3_close()</a>.
64More control over the execution of the database engine is provided
65using
66<a href="capi3ref.html#sqlite3_prepare">sqlite3_prepare()</a>
67to compile an SQLite statement into byte code and
68<a href="capi3ref.html#sqlite3_prepare">sqlite3_step()</a>
69to execute that bytecode.
70A family of routines with names beginning with
71<a href="capi3ref.html#sqlite3_column_blob">sqlite3_column_</a>
72is used to extract information about the result set of a query.
73Many interface functions come in pairs, with both a UTF-8 and
74UTF-16 version. And there is a collection of routines
75used to implement user-defined SQL functions and user-defined
76text collating sequences.
77</p>
78
79
80<h4>2.1 Opening and closing a database</h4>
81
82<blockquote><pre>
83 typedef struct sqlite3 sqlite3;
84 int sqlite3_open(const char*, sqlite3**);
85 int sqlite3_open16(const void*, sqlite3**);
86 int sqlite3_close(sqlite3*);
87 const char *sqlite3_errmsg(sqlite3*);
88 const void *sqlite3_errmsg16(sqlite3*);
89 int sqlite3_errcode(sqlite3*);
90</pre></blockquote>
91
92<p>
93The sqlite3_open() routine returns an integer error code rather than
94a pointer to the sqlite3 structure as the version 2 interface did.
95The difference between sqlite3_open()
96and sqlite3_open16() is that sqlite3_open16() takes UTF-16 (in host native
97byte order) for the name of the database file. If a new database file
98needs to be created, then sqlite3_open16() sets the internal text
99representation to UTF-16 whereas sqlite3_open() sets the text
100representation to UTF-8.
101</p>
102
103<p>
104The opening and/or creating of the database file is deferred until the
105file is actually needed. This allows options and parameters, such
106as the native text representation and default page size, to be
107set using PRAGMA statements.
108</p>
109
110<p>
111The sqlite3_errcode() routine returns a result code for the most
112recent major API call. sqlite3_errmsg() returns an English-language
113text error message for the most recent error. The error message is
114represented in UTF-8 and will be ephemeral - it could disappear on
115the next call to any SQLite API function. sqlite3_errmsg16() works like
116sqlite3_errmsg() except that it returns the error message represented
117as UTF-16 in host native byte order.
118</p>
119
120<p>
121The error codes for SQLite version 3 are unchanged from version 2.
122They are as follows:
123</p>
124
125<blockquote><pre>
126#define SQLITE_OK 0 /* Successful result */
127#define SQLITE_ERROR 1 /* SQL error or missing database */
128#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
129#define SQLITE_PERM 3 /* Access permission denied */
130#define SQLITE_ABORT 4 /* Callback routine requested an abort */
131#define SQLITE_BUSY 5 /* The database file is locked */
132#define SQLITE_LOCKED 6 /* A table in the database is locked */
133#define SQLITE_NOMEM 7 /* A malloc() failed */
134#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
135#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
136#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
137#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
138#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
139#define SQLITE_FULL 13 /* Insertion failed because database is full */
140#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
141#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
142#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
143#define SQLITE_SCHEMA 17 /* The database schema changed */
144#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
145#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
146#define SQLITE_MISMATCH 20 /* Data type mismatch */
147#define SQLITE_MISUSE 21 /* Library used incorrectly */
148#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
149#define SQLITE_AUTH 23 /* Authorization denied */
150#define SQLITE_ROW 100 /* sqlite_step() has another row ready */
151#define SQLITE_DONE 101 /* sqlite_step() has finished executing */
152</pre></blockquote>
153
154<h4>2.2 Executing SQL statements</h4>
155
156<blockquote><pre>
157 typedef int (*sqlite_callback)(void*,int,char**, char**);
158 int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);
159</pre></blockquote>
160
161<p>
162The sqlite3_exec function works much as it did in SQLite version 2.
163Zero or more SQL statements specified in the second parameter are compiled
164and executed. Query results are returned to a callback routine.
165See the <a href="capi3ref.html#sqlite3_exec">API reference</a> for additional
166information.
167</p>
168
169<p>
170In SQLite version 3, the sqlite3_exec routine is just a wrapper around
171calls to the prepared statement interface.
172</p>
173
174<blockquote><pre>
175 typedef struct sqlite3_stmt sqlite3_stmt;
176 int sqlite3_prepare(sqlite3*, const char*, int, sqlite3_stmt**, const char**);
177 int sqlite3_prepare16(sqlite3*, const void*, int, sqlite3_stmt**, const void**);
178 int sqlite3_finalize(sqlite3_stmt*);
179 int sqlite3_reset(sqlite3_stmt*);
180</pre></blockquote>
181
182<p>
183The sqlite3_prepare interface compiles a single SQL statement into byte code
184for later execution. This interface is now the preferred way of accessing
185the database.
186</p>
187
188<p>
189The SQL statement is a UTF-8 string for sqlite3_prepare().
190The sqlite3_prepare16() works the same way except
191that it expects a UTF-16 string as SQL input.
192Only the first SQL statement in the input string is compiled.
193The fourth parameter is filled in with a pointer to the next (uncompiled)
194SQLite statement in the input string, if any.
195The sqlite3_finalize() routine deallocates a prepared SQL statement.
196All prepared statements must be finalized before the database can be
197closed.
198The sqlite3_reset() routine resets a prepared SQL statement so that it
199can be executed again.
200</p>
201
202<p>
203The SQL statement may contain tokens of the form "?" or "?nnn" or ":aaa"
204where "nnn" is an integer and "aaa" is an identifier.
205Such tokens represent unspecified literal values (or "wildcards")
206to be filled in later by the
207<a href="capi3ref.html#sqlite3_bind_blob">sqlite3_bind</a> interface.
208Each wildcard has an associated number which is its sequence in the
209statement or the "nnn" in the case of a "?nnn" form.
210It is allowed for the same wildcard
211to occur more than once in the same SQL statement, in which case
212all instance of that wildcard will be filled in with the same value.
213Unbound wildcards have a value of NULL.
214</p>
215
216<blockquote><pre>
217 int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
218 int sqlite3_bind_double(sqlite3_stmt*, int, double);
219 int sqlite3_bind_int(sqlite3_stmt*, int, int);
220 int sqlite3_bind_int64(sqlite3_stmt*, int, long long int);
221 int sqlite3_bind_null(sqlite3_stmt*, int);
222 int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
223 int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
224 int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
225</pre></blockquote>
226
227<p>
228There is an assortment of sqlite3_bind routines used to assign values
229to wildcards in a prepared SQL statement. Unbound wildcards
230are interpreted as NULLs. Bindings are not reset by sqlite3_reset().
231But wildcards can be rebound to new values after an sqlite3_reset().
232</p>
233
234<p>
235After an SQL statement has been prepared (and optionally bound), it
236is executed using:
237</p>
238
239<blockquote><pre>
240 int sqlite3_step(sqlite3_stmt*);
241</pre></blockquote>
242
243<p>
244The sqlite3_step() routine return SQLITE_ROW if it is returning a single
245row of the result set, or SQLITE_DONE if execution has completed, either
246normally or due to an error. It might also return SQLITE_BUSY if it is
247unable to open the database file. If the return value is SQLITE_ROW, then
248the following routines can be used to extract information about that row
249of the result set:
250</p>
251
252<blockquote><pre>
253 const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
254 int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
255 int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
256 int sqlite3_column_count(sqlite3_stmt*);
257 const char *sqlite3_column_decltype(sqlite3_stmt *, int iCol);
258 const void *sqlite3_column_decltype16(sqlite3_stmt *, int iCol);
259 double sqlite3_column_double(sqlite3_stmt*, int iCol);
260 int sqlite3_column_int(sqlite3_stmt*, int iCol);
261 long long int sqlite3_column_int64(sqlite3_stmt*, int iCol);
262 const char *sqlite3_column_name(sqlite3_stmt*, int iCol);
263 const void *sqlite3_column_name16(sqlite3_stmt*, int iCol);
264 const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
265 const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
266 int sqlite3_column_type(sqlite3_stmt*, int iCol);
267</pre></blockquote>
268
269<p>
270The
271<a href="capi3ref.html#sqlite3_column_count">sqlite3_column_count()</a>
272function returns the number of columns in
273the results set. sqlite3_column_count() can be called at any time after
274sqlite3_prepare().
275<a href="capi3ref.html#sqlite3_data_count">sqlite3_data_count()</a>
276works similarly to
277sqlite3_column_count() except that it only works following sqlite3_step().
278If the previous call to sqlite3_step() returned SQLITE_DONE or an error code,
279then sqlite3_data_count() will return 0 whereas sqlite3_column_count() will
280continue to return the number of columns in the result set.
281</p>
282
283<p>Returned data is examined using the other sqlite3_column_***() functions,
284all of which take a column number as their second parameter. Columns are
285zero-indexed from left to right. Note that this is different to parameters,
286which are indexed starting at one.
287</p>
288
289<p>
290The sqlite3_column_type() function returns the
291datatype for the value in the Nth column. The return value is one
292of these:
293</p>
294
295<blockquote><pre>
296 #define SQLITE_INTEGER 1
297 #define SQLITE_FLOAT 2
298 #define SQLITE_TEXT 3
299 #define SQLITE_BLOB 4
300 #define SQLITE_NULL 5
301</pre></blockquote>
302
303<p>
304The sqlite3_column_decltype() routine returns text which is the
305declared type of the column in the CREATE TABLE statement. For an
306expression, the return type is an empty string. sqlite3_column_name()
307returns the name of the Nth column. sqlite3_column_bytes() returns
308the number of bytes in a column that has type BLOB or the number of bytes
309in a TEXT string with UTF-8 encoding. sqlite3_column_bytes16() returns
310the same value for BLOBs but for TEXT strings returns the number of bytes
311in a UTF-16 encoding.
312sqlite3_column_blob() return BLOB data.
313sqlite3_column_text() return TEXT data as UTF-8.
314sqlite3_column_text16() return TEXT data as UTF-16.
315sqlite3_column_int() return INTEGER data in the host machines native
316integer format.
317sqlite3_column_int64() returns 64-bit INTEGER data.
318Finally, sqlite3_column_double() return floating point data.
319</p>
320
321<p>
322It is not necessary to retrieve data in the format specify by
323sqlite3_column_type(). If a different format is requested, the data
324is converted automatically.
325</p>
326
327<p>
328Data format conversions can invalidate the pointer returned by
329prior calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
330sqlite3_column_text16(). Pointers might be invalided in the following
331cases:
332</p>
333<ul>
334<li><p>
335The initial content is a BLOB and sqlite3_column_text()
336or sqlite3_column_text16()
337is called. A zero-terminator might need to be added to the string.
338</p></li>
339<li><p>
340The initial content is UTF-8 text and sqlite3_column_bytes16() or
341sqlite3_column_text16() is called. The content must be converted to UTF-16.
342</p></li>
343<li><p>
344The initial content is UTF-16 text and sqlite3_column_bytes() or
345sqlite3_column_text() is called. The content must be converted to UTF-8.
346</p></li>
347</ul>
348<p>
349Note that conversions between UTF-16be and UTF-16le
350are always done in place and do
351not invalidate a prior pointer, though of course the content of the buffer
352that the prior pointer points to will have been modified. Other kinds
353of conversion are done in place when it is possible, but sometime it is
354not possible and in those cases prior pointers are invalidated.
355</p>
356
357<p>
358The safest and easiest to remember policy is this: assume that any
359result from
360<ul>
361<li>sqlite3_column_blob(),</li>
362<li>sqlite3_column_text(), or</li>
363<li>sqlite3_column_text16()</li>
364</ul>
365is invalided by subsequent calls to
366<ul>
367<li>sqlite3_column_bytes(),</li>
368<li>sqlite3_column_bytes16(),</li>
369<li>sqlite3_column_text(), or</li>
370<li>sqlite3_column_text16().</li>
371</ul>
372This means that you should always call sqlite3_column_bytes() or
373sqlite3_column_bytes16() <u>before</u> calling sqlite3_column_blob(),
374sqlite3_column_text(), or sqlite3_column_text16().
375</p>
376
377<h4>2.3 User-defined functions</h4>
378
379<p>
380User defined functions can be created using the following routine:
381</p>
382
383<blockquote><pre>
384 typedef struct sqlite3_value sqlite3_value;
385 int sqlite3_create_function(
386 sqlite3 *,
387 const char *zFunctionName,
388 int nArg,
389 int eTextRep,
390 void*,
391 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
392 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
393 void (*xFinal)(sqlite3_context*)
394 );
395 int sqlite3_create_function16(
396 sqlite3*,
397 const void *zFunctionName,
398 int nArg,
399 int eTextRep,
400 void*,
401 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
402 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
403 void (*xFinal)(sqlite3_context*)
404 );
405 #define SQLITE_UTF8 1
406 #define SQLITE_UTF16 2
407 #define SQLITE_UTF16BE 3
408 #define SQLITE_UTF16LE 4
409 #define SQLITE_ANY 5
410</pre></blockquote>
411
412<p>
413The nArg parameter specifies the number of arguments to the function.
414A value of 0 indicates that any number of arguments is allowed. The
415eTextRep parameter specifies what representation text values are expected
416to be in for arguments to this function. The value of this parameter should
417be one of the parameters defined above. SQLite version 3 allows multiple
418implementations of the same function using different text representations.
419The database engine chooses the function that minimization the number
420of text conversions required.
421</p>
422
423<p>
424Normal functions specify only xFunc and leave xStep and xFinal set to NULL.
425Aggregate functions specify xStep and xFinal and leave xFunc set to NULL.
426There is no separate sqlite3_create_aggregate() API.
427</p>
428
429<p>
430The function name is specified in UTF-8. A separate sqlite3_create_function16()
431API works the same as sqlite_create_function()
432except that the function name is specified in UTF-16 host byte order.
433</p>
434
435<p>
436Notice that the parameters to functions are now pointers to sqlite3_value
437structures instead of pointers to strings as in SQLite version 2.X.
438The following routines are used to extract useful information from these
439"values":
440</p>
441
442<blockquote><pre>
443 const void *sqlite3_value_blob(sqlite3_value*);
444 int sqlite3_value_bytes(sqlite3_value*);
445 int sqlite3_value_bytes16(sqlite3_value*);
446 double sqlite3_value_double(sqlite3_value*);
447 int sqlite3_value_int(sqlite3_value*);
448 long long int sqlite3_value_int64(sqlite3_value*);
449 const unsigned char *sqlite3_value_text(sqlite3_value*);
450 const void *sqlite3_value_text16(sqlite3_value*);
451 int sqlite3_value_type(sqlite3_value*);
452</pre></blockquote>
453
454<p>
455Function implementations use the following APIs to acquire context and
456to report results:
457</p>
458
459<blockquote><pre>
460 void *sqlite3_aggregate_context(sqlite3_context*, int nbyte);
461 void *sqlite3_user_data(sqlite3_context*);
462 void sqlite3_result_blob(sqlite3_context*, const void*, int n, void(*)(void*));
463 void sqlite3_result_double(sqlite3_context*, double);
464 void sqlite3_result_error(sqlite3_context*, const char*, int);
465 void sqlite3_result_error16(sqlite3_context*, const void*, int);
466 void sqlite3_result_int(sqlite3_context*, int);
467 void sqlite3_result_int64(sqlite3_context*, long long int);
468 void sqlite3_result_null(sqlite3_context*);
469 void sqlite3_result_text(sqlite3_context*, const char*, int n, void(*)(void*));
470 void sqlite3_result_text16(sqlite3_context*, const void*, int n, void(*)(void*));
471 void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
472 void *sqlite3_get_auxdata(sqlite3_context*, int);
473 void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
474</pre></blockquote>
475
476<h4>2.4 User-defined collating sequences</h4>
477
478<p>
479The following routines are used to implement user-defined
480collating sequences:
481</p>
482
483<blockquote><pre>
484 sqlite3_create_collation(sqlite3*, const char *zName, int eTextRep, void*,
485 int(*xCompare)(void*,int,const void*,int,const void*));
486 sqlite3_create_collation16(sqlite3*, const void *zName, int eTextRep, void*,
487 int(*xCompare)(void*,int,const void*,int,const void*));
488 sqlite3_collation_needed(sqlite3*, void*,
489 void(*)(void*,sqlite3*,int eTextRep,const char*));
490 sqlite3_collation_needed16(sqlite3*, void*,
491 void(*)(void*,sqlite3*,int eTextRep,const void*));
492</pre></blockquote>
493
494<p>
495The sqlite3_create_collation() function specifies a collating sequence name
496and a comparison function to implement that collating sequence. The
497comparison function is only used for comparing text values. The eTextRep
498parameter is one of SQLITE_UTF8, SQLITE_UTF16LE, SQLITE_UTF16BE, or
499SQLITE_ANY to specify which text representation the comparison function works
500with. Separate comparison functions can exist for the same collating
501sequence for each of the UTF-8, UTF-16LE and UTF-16BE text representations.
502The sqlite3_create_collation16() works like sqlite3_create_collation() except
503that the collation name is specified in UTF-16 host byte order instead of
504in UTF-8.
505</p>
506
507<p>
508The sqlite3_collation_needed() routine registers a callback which the
509database engine will invoke if it encounters an unknown collating sequence.
510The callback can lookup an appropriate comparison function and invoke
511sqlite_3_create_collation() as needed. The fourth parameter to the callback
512is the name of the collating sequence in UTF-8. For sqlite3_collation_need16()
513the callback sends the collating sequence name in UTF-16 host byte order.
514</p>
515}
516footer $rcsid