fix curls detection of ssl on AROS. Adjust the used link libraries (linking ssl twice...
[AROS-Contrib.git] / sqlite3 / www / capi3.tcl
blob2e1e0ec1809fef7d65be7bce2e97478e68e8b16b
1 set rcsid {$Id: capi3.tcl,v 1.9 2005/03/11 04:39:58 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>
327 <h4>2.3 User-defined functions</h4>
330 User defined functions can be created using the following routine:
331 </p>
333 <blockquote><pre>
334 typedef struct sqlite3_value sqlite3_value;
335 int sqlite3_create_function(
336 sqlite3 *,
337 const char *zFunctionName,
338 int nArg,
339 int eTextRep,
340 void*,
341 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
342 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
343 void (*xFinal)(sqlite3_context*)
345 int sqlite3_create_function16(
346 sqlite3*,
347 const void *zFunctionName,
348 int nArg,
349 int eTextRep,
350 void*,
351 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
352 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
353 void (*xFinal)(sqlite3_context*)
355 #define SQLITE_UTF8 1
356 #define SQLITE_UTF16 2
357 #define SQLITE_UTF16BE 3
358 #define SQLITE_UTF16LE 4
359 #define SQLITE_ANY 5
360 </pre></blockquote>
363 The nArg parameter specifies the number of arguments to the function.
364 A value of 0 indicates that any number of arguments is allowed. The
365 eTextRep parameter specifies what representation text values are expected
366 to be in for arguments to this function. The value of this parameter should
367 be one of the parameters defined above. SQLite version 3 allows multiple
368 implementations of the same function using different text representations.
369 The database engine chooses the function that minimization the number
370 of text conversions required.
371 </p>
374 Normal functions specify only xFunc and leave xStep and xFinal set to NULL.
375 Aggregate functions specify xStep and xFinal and leave xFunc set to NULL.
376 There is no separate sqlite3_create_aggregate() API.
377 </p>
380 The function name is specified in UTF-8. A separate sqlite3_create_function16()
381 API works the same as sqlite_create_function()
382 except that the function name is specified in UTF-16 host byte order.
383 </p>
386 Notice that the parameters to functions are now pointers to sqlite3_value
387 structures instead of pointers to strings as in SQLite version 2.X.
388 The following routines are used to extract useful information from these
389 "values":
390 </p>
392 <blockquote><pre>
393 const void *sqlite3_value_blob(sqlite3_value*);
394 int sqlite3_value_bytes(sqlite3_value*);
395 int sqlite3_value_bytes16(sqlite3_value*);
396 double sqlite3_value_double(sqlite3_value*);
397 int sqlite3_value_int(sqlite3_value*);
398 long long int sqlite3_value_int64(sqlite3_value*);
399 const unsigned char *sqlite3_value_text(sqlite3_value*);
400 const void *sqlite3_value_text16(sqlite3_value*);
401 int sqlite3_value_type(sqlite3_value*);
402 </pre></blockquote>
405 Function implementations use the following APIs to acquire context and
406 to report results:
407 </p>
409 <blockquote><pre>
410 void *sqlite3_aggregate_context(sqlite3_context*, int nbyte);
411 void *sqlite3_user_data(sqlite3_context*);
412 void sqlite3_result_blob(sqlite3_context*, const void*, int n, void(*)(void*));
413 void sqlite3_result_double(sqlite3_context*, double);
414 void sqlite3_result_error(sqlite3_context*, const char*, int);
415 void sqlite3_result_error16(sqlite3_context*, const void*, int);
416 void sqlite3_result_int(sqlite3_context*, int);
417 void sqlite3_result_int64(sqlite3_context*, long long int);
418 void sqlite3_result_null(sqlite3_context*);
419 void sqlite3_result_text(sqlite3_context*, const char*, int n, void(*)(void*));
420 void sqlite3_result_text16(sqlite3_context*, const void*, int n, void(*)(void*));
421 void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
422 void *sqlite3_get_auxdata(sqlite3_context*, int);
423 void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
424 </pre></blockquote>
426 <h4>2.4 User-defined collating sequences</h4>
429 The following routines are used to implement user-defined
430 collating sequences:
431 </p>
433 <blockquote><pre>
434 sqlite3_create_collation(sqlite3*, const char *zName, int eTextRep, void*,
435 int(*xCompare)(void*,int,const void*,int,const void*));
436 sqlite3_create_collation16(sqlite3*, const void *zName, int eTextRep, void*,
437 int(*xCompare)(void*,int,const void*,int,const void*));
438 sqlite3_collation_needed(sqlite3*, void*,
439 void(*)(void*,sqlite3*,int eTextRep,const char*));
440 sqlite3_collation_needed16(sqlite3*, void*,
441 void(*)(void*,sqlite3*,int eTextRep,const void*));
442 </pre></blockquote>
445 The sqlite3_create_collation() function specifies a collating sequence name
446 and a comparison function to implement that collating sequence. The
447 comparison function is only used for comparing text values. The eTextRep
448 parameter is one of SQLITE_UTF8, SQLITE_UTF16LE, SQLITE_UTF16BE, or
449 SQLITE_ANY to specify which text representation the comparison function works
450 with. Separate comparison functions can exist for the same collating
451 sequence for each of the UTF-8, UTF-16LE and UTF-16BE text representations.
452 The sqlite3_create_collation16() works like sqlite3_create_collation() except
453 that the collation name is specified in UTF-16 host byte order instead of
454 in UTF-8.
455 </p>
458 The sqlite3_collation_needed() routine registers a callback which the
459 database engine will invoke if it encounters an unknown collating sequence.
460 The callback can lookup an appropriate comparison function and invoke
461 sqlite_3_create_collation() as needed. The fourth parameter to the callback
462 is the name of the collating sequence in UTF-8. For sqlite3_collation_need16()
463 the callback sends the collating sequence name in UTF-16 host byte order.
464 </p>
466 footer $rcsid