[Author: zork]
[google-gears.git] / gears / third_party / sqlite_google / www / capi3.tcl
blob149cf7f0adc7f480fde0f70aceec240800238ba5
1 set rcsid {$Id: capi3.tcl,v 1.10 2007/04/27 17:16:22 drh Exp $}
2 source common.tcl
3 header {C/C++ Interface For SQLite Version 3}
5 proc AddHyperlinks {txt} {
6 regsub -all {([^:alnum:>])(sqlite3_\w+)(\([^\)]*\))} $txt \
7 {\1<a href="capi3ref.html#\2">\2</a>\3} t2
8 puts $t2
11 AddHyperlinks {
12 <h2>C/C++ Interface For SQLite Version 3</h2>
14 <h3>1.0 Overview</h3>
16 <p>
17 SQLite version 3.0 is a new version of SQLite, derived from
18 the SQLite 2.8.13 code base, but with an incompatible file format
19 and API.
20 SQLite version 3.0 was created to answer demand for the following features:
21 </p>
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>
29 <p>
30 It was necessary to move to version 3.0 to implement these features because
31 each requires incompatible changes to the database file format. Other
32 incompatible changes, such as a cleanup of the API, were introduced at the
33 same time under the theory that it is best to get your incompatible changes
34 out of the way all at once.
35 </p>
37 <p>
38 The API for version 3.0 is similar to the version 2.X API,
39 but with some important changes. Most noticeably, the "<tt>sqlite_</tt>"
40 prefix that occurs on the beginning of all API functions and data
41 structures are changed to "<tt>sqlite3_</tt>".
42 This avoids confusion between the two APIs and allows linking against both
43 SQLite 2.X and SQLite 3.0 at the same time.
44 </p>
46 <p>
47 There is no agreement on what the C datatype for a UTF-16
48 string should be. Therefore, SQLite uses a generic type of void*
49 to refer to UTF-16 strings. Client software can cast the void*
50 to whatever datatype is appropriate for their system.
51 </p>
53 <h3>2.0 C/C++ Interface</h3>
55 <p>
56 The API for SQLite 3.0 includes 83 separate functions in addition
57 to several data structures and #defines. (A complete
58 <a href="capi3ref.html">API reference</a> is provided as a separate document.)
59 Fortunately, the interface is not nearly as complex as its size implies.
60 Simple 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>.
64 More control over the execution of the database engine is provided
65 using
66 <a href="capi3ref.html#sqlite3_prepare">sqlite3_prepare()</a>
67 to compile an SQLite statement into byte code and
68 <a href="capi3ref.html#sqlite3_prepare">sqlite3_step()</a>
69 to execute that bytecode.
70 A family of routines with names beginning with
71 <a href="capi3ref.html#sqlite3_column_blob">sqlite3_column_</a>
72 is used to extract information about the result set of a query.
73 Many interface functions come in pairs, with both a UTF-8 and
74 UTF-16 version. And there is a collection of routines
75 used to implement user-defined SQL functions and user-defined
76 text collating sequences.
77 </p>
80 <h4>2.1 Opening and closing a database</h4>
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>
92 <p>
93 The sqlite3_open() routine returns an integer error code rather than
94 a pointer to the sqlite3 structure as the version 2 interface did.
95 The difference between sqlite3_open()
96 and sqlite3_open16() is that sqlite3_open16() takes UTF-16 (in host native
97 byte order) for the name of the database file. If a new database file
98 needs to be created, then sqlite3_open16() sets the internal text
99 representation to UTF-16 whereas sqlite3_open() sets the text
100 representation to UTF-8.
101 </p>
104 The opening and/or creating of the database file is deferred until the
105 file is actually needed. This allows options and parameters, such
106 as the native text representation and default page size, to be
107 set using PRAGMA statements.
108 </p>
111 The sqlite3_errcode() routine returns a result code for the most
112 recent major API call. sqlite3_errmsg() returns an English-language
113 text error message for the most recent error. The error message is
114 represented in UTF-8 and will be ephemeral - it could disappear on
115 the next call to any SQLite API function. sqlite3_errmsg16() works like
116 sqlite3_errmsg() except that it returns the error message represented
117 as UTF-16 in host native byte order.
118 </p>
121 The error codes for SQLite version 3 are unchanged from version 2.
122 They are as follows:
123 </p>
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>
154 <h4>2.2 Executing SQL statements</h4>
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>
162 The sqlite3_exec function works much as it did in SQLite version 2.
163 Zero or more SQL statements specified in the second parameter are compiled
164 and executed. Query results are returned to a callback routine.
165 See the <a href="capi3ref.html#sqlite3_exec">API reference</a> for additional
166 information.
167 </p>
170 In SQLite version 3, the sqlite3_exec routine is just a wrapper around
171 calls to the prepared statement interface.
172 </p>
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>
183 The sqlite3_prepare interface compiles a single SQL statement into byte code
184 for later execution. This interface is now the preferred way of accessing
185 the database.
186 </p>
189 The SQL statement is a UTF-8 string for sqlite3_prepare().
190 The sqlite3_prepare16() works the same way except
191 that it expects a UTF-16 string as SQL input.
192 Only the first SQL statement in the input string is compiled.
193 The fourth parameter is filled in with a pointer to the next (uncompiled)
194 SQLite statement in the input string, if any.
195 The sqlite3_finalize() routine deallocates a prepared SQL statement.
196 All prepared statements must be finalized before the database can be
197 closed.
198 The sqlite3_reset() routine resets a prepared SQL statement so that it
199 can be executed again.
200 </p>
203 The SQL statement may contain tokens of the form "?" or "?nnn" or ":aaa"
204 where "nnn" is an integer and "aaa" is an identifier.
205 Such tokens represent unspecified literal values (or "wildcards")
206 to be filled in later by the
207 <a href="capi3ref.html#sqlite3_bind_blob">sqlite3_bind</a> interface.
208 Each wildcard has an associated number which is its sequence in the
209 statement or the "nnn" in the case of a "?nnn" form.
210 It is allowed for the same wildcard
211 to occur more than once in the same SQL statement, in which case
212 all instance of that wildcard will be filled in with the same value.
213 Unbound wildcards have a value of NULL.
214 </p>
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>
228 There is an assortment of sqlite3_bind routines used to assign values
229 to wildcards in a prepared SQL statement. Unbound wildcards
230 are interpreted as NULLs. Bindings are not reset by sqlite3_reset().
231 But wildcards can be rebound to new values after an sqlite3_reset().
232 </p>
235 After an SQL statement has been prepared (and optionally bound), it
236 is executed using:
237 </p>
239 <blockquote><pre>
240 int sqlite3_step(sqlite3_stmt*);
241 </pre></blockquote>
244 The sqlite3_step() routine return SQLITE_ROW if it is returning a single
245 row of the result set, or SQLITE_DONE if execution has completed, either
246 normally or due to an error. It might also return SQLITE_BUSY if it is
247 unable to open the database file. If the return value is SQLITE_ROW, then
248 the following routines can be used to extract information about that row
249 of the result set:
250 </p>
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>
270 The
271 <a href="capi3ref.html#sqlite3_column_count">sqlite3_column_count()</a>
272 function returns the number of columns in
273 the results set. sqlite3_column_count() can be called at any time after
274 sqlite3_prepare().
275 <a href="capi3ref.html#sqlite3_data_count">sqlite3_data_count()</a>
276 works similarly to
277 sqlite3_column_count() except that it only works following sqlite3_step().
278 If the previous call to sqlite3_step() returned SQLITE_DONE or an error code,
279 then sqlite3_data_count() will return 0 whereas sqlite3_column_count() will
280 continue to return the number of columns in the result set.
281 </p>
283 <p>Returned data is examined using the other sqlite3_column_***() functions,
284 all of which take a column number as their second parameter. Columns are
285 zero-indexed from left to right. Note that this is different to parameters,
286 which are indexed starting at one.
287 </p>
290 The sqlite3_column_type() function returns the
291 datatype for the value in the Nth column. The return value is one
292 of these:
293 </p>
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>
304 The sqlite3_column_decltype() routine returns text which is the
305 declared type of the column in the CREATE TABLE statement. For an
306 expression, the return type is an empty string. sqlite3_column_name()
307 returns the name of the Nth column. sqlite3_column_bytes() returns
308 the number of bytes in a column that has type BLOB or the number of bytes
309 in a TEXT string with UTF-8 encoding. sqlite3_column_bytes16() returns
310 the same value for BLOBs but for TEXT strings returns the number of bytes
311 in a UTF-16 encoding.
312 sqlite3_column_blob() return BLOB data.
313 sqlite3_column_text() return TEXT data as UTF-8.
314 sqlite3_column_text16() return TEXT data as UTF-16.
315 sqlite3_column_int() return INTEGER data in the host machines native
316 integer format.
317 sqlite3_column_int64() returns 64-bit INTEGER data.
318 Finally, sqlite3_column_double() return floating point data.
319 </p>
322 It is not necessary to retrieve data in the format specify by
323 sqlite3_column_type(). If a different format is requested, the data
324 is converted automatically.
325 </p>
328 Data format conversions can invalidate the pointer returned by
329 prior calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
330 sqlite3_column_text16(). Pointers might be invalided in the following
331 cases:
332 </p>
333 <ul>
334 <li><p>
335 The initial content is a BLOB and sqlite3_column_text()
336 or sqlite3_column_text16()
337 is called. A zero-terminator might need to be added to the string.
338 </p></li>
339 <li><p>
340 The initial content is UTF-8 text and sqlite3_column_bytes16() or
341 sqlite3_column_text16() is called. The content must be converted to UTF-16.
342 </p></li>
343 <li><p>
344 The initial content is UTF-16 text and sqlite3_column_bytes() or
345 sqlite3_column_text() is called. The content must be converted to UTF-8.
346 </p></li>
347 </ul>
349 Note that conversions between UTF-16be and UTF-16le
350 are always done in place and do
351 not invalidate a prior pointer, though of course the content of the buffer
352 that the prior pointer points to will have been modified. Other kinds
353 of conversion are done in place when it is possible, but sometime it is
354 not possible and in those cases prior pointers are invalidated.
355 </p>
358 The safest and easiest to remember policy is this: assume that any
359 result from
360 <ul>
361 <li>sqlite3_column_blob(),</li>
362 <li>sqlite3_column_text(), or</li>
363 <li>sqlite3_column_text16()</li>
364 </ul>
365 is 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>
372 This means that you should always call sqlite3_column_bytes() or
373 sqlite3_column_bytes16() <u>before</u> calling sqlite3_column_blob(),
374 sqlite3_column_text(), or sqlite3_column_text16().
375 </p>
377 <h4>2.3 User-defined functions</h4>
380 User defined functions can be created using the following routine:
381 </p>
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*)
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*)
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>
413 The nArg parameter specifies the number of arguments to the function.
414 A value of 0 indicates that any number of arguments is allowed. The
415 eTextRep parameter specifies what representation text values are expected
416 to be in for arguments to this function. The value of this parameter should
417 be one of the parameters defined above. SQLite version 3 allows multiple
418 implementations of the same function using different text representations.
419 The database engine chooses the function that minimization the number
420 of text conversions required.
421 </p>
424 Normal functions specify only xFunc and leave xStep and xFinal set to NULL.
425 Aggregate functions specify xStep and xFinal and leave xFunc set to NULL.
426 There is no separate sqlite3_create_aggregate() API.
427 </p>
430 The function name is specified in UTF-8. A separate sqlite3_create_function16()
431 API works the same as sqlite_create_function()
432 except that the function name is specified in UTF-16 host byte order.
433 </p>
436 Notice that the parameters to functions are now pointers to sqlite3_value
437 structures instead of pointers to strings as in SQLite version 2.X.
438 The following routines are used to extract useful information from these
439 "values":
440 </p>
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>
455 Function implementations use the following APIs to acquire context and
456 to report results:
457 </p>
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>
476 <h4>2.4 User-defined collating sequences</h4>
479 The following routines are used to implement user-defined
480 collating sequences:
481 </p>
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>
495 The sqlite3_create_collation() function specifies a collating sequence name
496 and a comparison function to implement that collating sequence. The
497 comparison function is only used for comparing text values. The eTextRep
498 parameter is one of SQLITE_UTF8, SQLITE_UTF16LE, SQLITE_UTF16BE, or
499 SQLITE_ANY to specify which text representation the comparison function works
500 with. Separate comparison functions can exist for the same collating
501 sequence for each of the UTF-8, UTF-16LE and UTF-16BE text representations.
502 The sqlite3_create_collation16() works like sqlite3_create_collation() except
503 that the collation name is specified in UTF-16 host byte order instead of
504 in UTF-8.
505 </p>
508 The sqlite3_collation_needed() routine registers a callback which the
509 database engine will invoke if it encounters an unknown collating sequence.
510 The callback can lookup an appropriate comparison function and invoke
511 sqlite_3_create_collation() as needed. The fourth parameter to the callback
512 is the name of the collating sequence in UTF-8. For sqlite3_collation_need16()
513 the callback sends the collating sequence name in UTF-16 host byte order.
514 </p>
516 footer $rcsid