c_interface.tcl 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. #
  2. # Run this Tcl script to generate the sqlite.html file.
  3. #
  4. set rcsid {$Id: c_interface.tcl,v 1.30 2002/06/16 04:57:32 chw Exp $}
  5. puts {<html>
  6. <head>
  7. <title>The C language interface to the SQLite library</title>
  8. </head>
  9. <body bgcolor=white>
  10. <h1 align=center>
  11. The C language interface to the SQLite library
  12. </h1>}
  13. puts "<p align=center>
  14. (This page was last modified on [lrange $rcsid 3 4] UTC)
  15. </p>"
  16. puts {
  17. <p>The SQLite library is designed to be very easy to use from
  18. a C or C++ program. This document gives an overview of the C/C++
  19. programming interface.</p>
  20. <h2>The Core API</h2>
  21. <p>The interface to the SQLite library consists of three core functions,
  22. one opaque data structure, and some constants used as return values.
  23. The core interface is as follows:</p>
  24. <blockquote><pre>
  25. typedef struct sqlite sqlite;
  26. #define SQLITE_OK 0 /* Successful result */
  27. sqlite *sqlite_open(const char *dbname, int mode, char **errmsg);
  28. void sqlite_close(sqlite*);
  29. int sqlite_exec(
  30. sqlite*,
  31. char *sql,
  32. int (*)(void*,int,char**,char**),
  33. void*,
  34. char **errmsg
  35. );
  36. </pre></blockquote>
  37. <p>
  38. The above is all you really need to know in order to use SQLite
  39. in your C or C++ programs. There are other convenience functions
  40. available (and described below) but we will begin by describing
  41. the core functions shown above.
  42. </p>
  43. <h2>Opening a database</h2>
  44. <p>Use the <b>sqlite_open()</b> function to open an existing SQLite
  45. database or to create a new SQLite database. The first argument
  46. is the database name. The second argument is intended to signal
  47. whether the database is going to be used for reading and writing
  48. or just for reading. But in the current implementation, the
  49. second argument to <b>sqlite_open</b> is ignored.
  50. The third argument is a pointer to a string pointer.
  51. If the third argument is not NULL and an error occurs
  52. while trying to open the database, then an error message will be
  53. written to memory obtained from malloc() and *errmsg will be made
  54. to point to this error message. The calling function is responsible
  55. for freeing the memory when it has finished with it.</p>
  56. <p>The name of an SQLite database is the name of a file that will
  57. contain the database. If the file does not exist, SQLite attempts
  58. to create and initialize it. If the file is read-only (due to
  59. permission bits or because it is located on read-only media like
  60. a CD-ROM) then SQLite opens the database for reading only. The
  61. entire SQL database is stored in a single file on the disk. But
  62. additional temporary files may be created during the execution of
  63. an SQL command in order to store the database rollback journal or
  64. temporary and intermediate results of a query.</p>
  65. <p>The return value of the <b>sqlite_open()</b> function is a
  66. pointer to an opaque <b>sqlite</b> structure. This pointer will
  67. be the first argument to all subsequent SQLite function calls that
  68. deal with the same database. NULL is returned if the open fails
  69. for any reason.</p>
  70. <h2>Closing the database</h2>
  71. <p>To close an SQLite database, call the <b>sqlite_close()</b>
  72. function passing it the sqlite structure pointer that was obtained
  73. from a prior call to <b>sqlite_open</b>.
  74. If a transaction is active when the database is closed, the transaction
  75. is rolled back.</p>
  76. <h2>Executing SQL statements</h2>
  77. <p>The <b>sqlite_exec()</b> function is used to process SQL statements
  78. and queries. This function requires 5 parameters as follows:</p>
  79. <ol>
  80. <li><p>A pointer to the sqlite structure obtained from a prior call
  81. to <b>sqlite_open()</b>.</p></li>
  82. <li><p>A null-terminated string containing the text of one or more
  83. SQL statements and/or queries to be processed.</p></li>
  84. <li><p>A pointer to a callback function which is invoked once for each
  85. row in the result of a query. This argument may be NULL, in which
  86. case no callbacks will ever be invoked.</p></li>
  87. <li><p>A pointer that is forwarded to become the first argument
  88. to the callback function.</p></li>
  89. <li><p>A pointer to an error string. Error messages are written to space
  90. obtained from malloc() and the error string is made to point to
  91. the malloced space. The calling function is responsible for freeing
  92. this space when it has finished with it.
  93. This argument may be NULL, in which case error messages are not
  94. reported back to the calling function.</p></li>
  95. </ol>
  96. <p>
  97. The callback function is used to receive the results of a query. A
  98. prototype for the callback function is as follows:</p>
  99. <blockquote><pre>
  100. int Callback(void *pArg, int argc, char **argv, char **columnNames){
  101. return 0;
  102. }
  103. </pre></blockquote>
  104. <p>The first argument to the callback is just a copy of the fourth argument
  105. to <b>sqlite_exec()</b> This parameter can be used to pass arbitrary
  106. information through to the callback function from client code.
  107. The second argument is the number of columns in the query result.
  108. The third argument is an array of pointers to strings where each string
  109. is a single column of the result for that record. Note that the
  110. callback function reports a NULL value in the database as a NULL pointer,
  111. which is very different from an empty string. If the i-th parameter
  112. is an empty string, we will get:</p>
  113. <blockquote><pre>
  114. argv[i][0] == 0
  115. </pre></blockquote>
  116. <p>But if the i-th parameter is NULL we will get:</p>
  117. <blockquote><pre>
  118. argv[i] == 0
  119. </pre></blockquote>
  120. <p>The names of the columns are contained in the fourth argument.</p>
  121. <p>If the EMPTY_RESULT_CALLBACKS pragma is set to ON and the result of
  122. a query is an empty set, then the callback is invoked once with the
  123. third parameter (argv) set to 0. In other words
  124. <blockquote><pre>
  125. argv == 0
  126. </pre></blockquote>
  127. The second parameter (argc)
  128. and the fourth parameter (columnNames) are still valid
  129. and can be used to determine the number and names of the result
  130. columns if there had been a result.
  131. The default behavior is not to invoke the callback at all if the
  132. result set is empty.</p>
  133. <p>The callback function should normally return 0. If the callback
  134. function returns non-zero, the query is immediately aborted and
  135. <b>sqlite_exec()</b> will return SQLITE_ABORT.</p>
  136. <h2>Error Codes</h2>
  137. <p>
  138. The <b>sqlite_exec()</b> function normally returns SQLITE_OK. But
  139. if something goes wrong it can return a different value to indicate
  140. the type of error. Here is a complete list of the return codes:
  141. </p>
  142. <blockquote><pre>
  143. #define SQLITE_OK 0 /* Successful result */
  144. #define SQLITE_ERROR 1 /* SQL error or missing database */
  145. #define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
  146. #define SQLITE_PERM 3 /* Access permission denied */
  147. #define SQLITE_ABORT 4 /* Callback routine requested an abort */
  148. #define SQLITE_BUSY 5 /* The database file is locked */
  149. #define SQLITE_LOCKED 6 /* A table in the database is locked */
  150. #define SQLITE_NOMEM 7 /* A malloc() failed */
  151. #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
  152. #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
  153. #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
  154. #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
  155. #define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
  156. #define SQLITE_FULL 13 /* Insertion failed because database is full */
  157. #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
  158. #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
  159. #define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
  160. #define SQLITE_SCHEMA 17 /* The database schema changed */
  161. #define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
  162. #define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
  163. #define SQLITE_MISMATCH 20 /* Data type mismatch */
  164. #define SQLITE_MISUSE 21 /* Library used incorrectly */
  165. </pre></blockquote>
  166. <p>
  167. The meanings of these various return values are as follows:
  168. </p>
  169. <blockquote>
  170. <dl>
  171. <dt>SQLITE_OK</dt>
  172. <dd><p>This value is returned if everything worked and there were no errors.
  173. </p></dd>
  174. <dt>SQLITE_INTERNAL</dt>
  175. <dd><p>This value indicates that an internal consistency check within
  176. the SQLite library failed. This can only happen if there is a bug in
  177. the SQLite library. If you ever get an SQLITE_INTERNAL reply from
  178. an <b>sqlite_exec()</b> call, please report the problem on the SQLite
  179. mailing list.
  180. </p></dd>
  181. <dt>SQLITE_ERROR</dt>
  182. <dd><p>This return value indicates that there was an error in the SQL
  183. that was passed into the <b>sqlite_exec()</b>.
  184. </p></dd>
  185. <dt>SQLITE_PERM</dt>
  186. <dd><p>This return value says that the access permissions on the database
  187. file are such that the file cannot be opened.
  188. </p></dd>
  189. <dt>SQLITE_ABORT</dt>
  190. <dd><p>This value is returned if the callback function returns non-zero.
  191. </p></dd>
  192. <dt>SQLITE_BUSY</dt>
  193. <dd><p>This return code indicates that another program or thread has
  194. the database locked. SQLite allows two or more threads to read the
  195. database at the same time, but only one thread can have the database
  196. open for writing at the same time. Locking in SQLite is on the
  197. entire database.</p>
  198. </p></dd>
  199. <dt>SQLITE_LOCKED</dt>
  200. <dd><p>This return code is similar to SQLITE_BUSY in that it indicates
  201. that the database is locked. But the source of the lock is a recursive
  202. call to <b>sqlite_exec()</b>. This return can only occur if you attempt
  203. to invoke sqlite_exec() from within a callback routine of a query
  204. from a prior invocation of sqlite_exec(). Recursive calls to
  205. sqlite_exec() are allowed as long as they do
  206. not attempt to write the same table.
  207. </p></dd>
  208. <dt>SQLITE_NOMEM</dt>
  209. <dd><p>This value is returned if a call to <b>malloc()</b> fails.
  210. </p></dd>
  211. <dt>SQLITE_READONLY</dt>
  212. <dd><p>This return code indicates that an attempt was made to write to
  213. a database file that is opened for reading only.
  214. </p></dd>
  215. <dt>SQLITE_INTERRUPT</dt>
  216. <dd><p>This value is returned if a call to <b>sqlite_interrupt()</b>
  217. interrupts a database operation in progress.
  218. </p></dd>
  219. <dt>SQLITE_IOERR</dt>
  220. <dd><p>This value is returned if the operating system informs SQLite
  221. that it is unable to perform some disk I/O operation. This could mean
  222. that there is no more space left on the disk.
  223. </p></dd>
  224. <dt>SQLITE_CORRUPT</dt>
  225. <dd><p>This value is returned if SQLite detects that the database it is
  226. working on has become corrupted. Corruption might occur due to a rogue
  227. process writing to the database file or it might happen due to an
  228. perviously undetected logic error in of SQLite. This value is also
  229. returned if a disk I/O error occurs in such a way that SQLite is forced
  230. to leave the database file in a corrupted state. The latter should only
  231. happen due to a hardware or operating system malfunction.
  232. </p></dd>
  233. <dt>SQLITE_FULL</dt>
  234. <dd><p>This value is returned if an insertion failed because there is
  235. no space left on the disk, or the database is too big to hold any
  236. more information. The latter case should only occur for databases
  237. that are larger than 2GB in size.
  238. </p></dd>
  239. <dt>SQLITE_CANTOPEN</dt>
  240. <dd><p>This value is returned if the database file could not be opened
  241. for some reason.
  242. </p></dd>
  243. <dt>SQLITE_PROTOCOL</dt>
  244. <dd><p>This value is returned if some other process is messing with
  245. file locks and has violated the file locking protocol that SQLite uses
  246. on its rollback journal files.
  247. </p></dd>
  248. <dt>SQLITE_SCHEMA</dt>
  249. <dd><p>When the database first opened, SQLite reads the database schema
  250. into memory and uses that schema to parse new SQL statements. If another
  251. process changes the schema, the command currently being processed will
  252. abort because the virtual machine code generated assumed the old
  253. schema. This is the return code for such cases. Retrying the
  254. command usually will clear the problem.
  255. </p></dd>
  256. <dt>SQLITE_TOOBIG</dt>
  257. <dd><p>SQLite will not store more than about 1 megabyte of data in a single
  258. row of a single table. If you attempt to store more than 1 megabyte
  259. in a single row, this is the return code you get.
  260. </p></dd>
  261. <dt>SQLITE_CONSTRAINT</dt>
  262. <dd><p>This constant is returned if the SQL statement would have violated
  263. a database constraint.
  264. </p></dd>
  265. <dt>SQLITE_MISMATCH</dt>
  266. <dd><p>This error occurs when there is an attempt to insert non-integer
  267. data into a column labeled INTEGER PRIMARY KEY. For most columns, SQLite
  268. ignores the data type and allows any kind of data to be stored. But
  269. an INTEGER PRIMARY KEY column is only allowed to store integer data.
  270. </p></dd>
  271. <dt>SQLITE_MISUSE</dt>
  272. <dd><p>This error might occur if one or more of the SQLite API routines
  273. is used incorrectly. Examples of incorrect usage include calling
  274. <b>sqlite_exec()</b> after the database has been closed using
  275. <b>sqlite_close()</b> or calling <b>sqlite_exec()</b> with the same
  276. database pointer simultaneously from two separate threads.
  277. </p></dd>
  278. </dl>
  279. </blockquote>
  280. <h2>The Extended API</h2>
  281. <p>Only the three core routines shown above are required to use
  282. SQLite. But there are many other functions that provide
  283. useful interfaces. These extended routines are as follows:
  284. </p>
  285. <blockquote><pre>
  286. int sqlite_last_insert_rowid(sqlite*);
  287. int sqlite_changes(sqlite*);
  288. int sqlite_get_table(
  289. sqlite*,
  290. char *sql,
  291. char ***result,
  292. int *nrow,
  293. int *ncolumn,
  294. char **errmsg
  295. );
  296. void sqlite_free_table(char**);
  297. void sqlite_interrupt(sqlite*);
  298. int sqlite_complete(const char *sql);
  299. void sqlite_busy_handler(sqlite*, int (*)(void*,const char*,int), void*);
  300. void sqlite_busy_timeout(sqlite*, int ms);
  301. const char sqlite_version[];
  302. const char sqlite_encoding[];
  303. int sqlite_exec_printf(
  304. sqlite*,
  305. char *sql,
  306. int (*)(void*,int,char**,char**),
  307. void*,
  308. char **errmsg,
  309. ...
  310. );
  311. int sqlite_exec_vprintf(
  312. sqlite*,
  313. char *sql,
  314. int (*)(void*,int,char**,char**),
  315. void*,
  316. char **errmsg,
  317. va_list
  318. );
  319. int sqlite_get_table_printf(
  320. sqlite*,
  321. char *sql,
  322. char ***result,
  323. int *nrow,
  324. int *ncolumn,
  325. char **errmsg,
  326. ...
  327. );
  328. int sqlite_get_table_vprintf(
  329. sqlite*,
  330. char *sql,
  331. char ***result,
  332. int *nrow,
  333. int *ncolumn,
  334. char **errmsg,
  335. va_list
  336. );
  337. </pre></blockquote>
  338. <p>All of the above definitions are included in the "sqlite.h"
  339. header file that comes in the source tree.</p>
  340. <h2>The ROWID of the most recent insert</h2>
  341. <p>Every row of an SQLite table has a unique integer key. If the
  342. table has a column labeled INTEGER PRIMARY KEY, then that column
  343. serves as the key. If there is no INTEGER PRIMARY KEY column then
  344. the key is a unique integer. The key for a row can be accessed in
  345. a SELECT statement or used in a WHERE or ORDER BY clause using any
  346. of the names "ROWID", "OID", or "_ROWID_".</p>
  347. <p>When you do an insert into a table that does not have an INTEGER PRIMARY
  348. KEY column, or if the table does have an INTEGER PRIMARY KEY but the value
  349. for that column is not specified in the VALUES clause of the insert, then
  350. the key is automatically generated. You can find the value of the key
  351. for the most recent INSERT statement using the
  352. <b>sqlite_last_insert_rowid()</b> API function.</p>
  353. <h2>The number of rows that changed</h2>
  354. <p>The <b>sqlite_changes()</b> API function returns the number of rows
  355. that were inserted, deleted, or modified during the most recent
  356. <b>sqlite_exec()</b> call. The number reported includes any changes
  357. that were later undo by a ROLLBACK or ABORT. But rows that are
  358. deleted because of a DROP TABLE are <em>not</em> counted.</p>
  359. <p>SQLite implements the command "<b>DELETE FROM table</b>" (without
  360. a WHERE clause) by dropping the table then recreating it.
  361. This is much faster than deleting the elements of the table individually.
  362. But it also means that the value returned from <b>sqlite_changes()</b>
  363. will be zero regardless of the number of elements that were originally
  364. in the table. If an accurate count of the number of elements deleted
  365. is necessary, use "<b>DELETE FROM table WHERE 1</b>" instead.</p>
  366. <h2>Querying without using a callback function</h2>
  367. <p>The <b>sqlite_get_table()</b> function is a wrapper around
  368. <b>sqlite_exec()</b> that collects all the information from successive
  369. callbacks and write it into memory obtained from malloc(). This
  370. is a convenience function that allows the application to get the
  371. entire result of a database query with a single function call.</p>
  372. <p>The main result from <b>sqlite_get_table()</b> is an array of pointers
  373. to strings. There is one element in this array for each column of
  374. each row in the result. NULL results are represented by a NULL
  375. pointer. In addition to the regular data, there is an added row at the
  376. beginning of the array that contains the names of each column of the
  377. result.</p>
  378. <p>As an example, consider the following query:</p>
  379. <blockquote>
  380. SELECT employee_name, login, host FROM users WHERE logic LIKE 'd%';
  381. </blockquote>
  382. <p>This query will return the name, login and host computer name
  383. for every employee whose login begins with the letter "d". If this
  384. query is submitted to <b>sqlite_get_table()</b> the result might
  385. look like this:</p>
  386. <blockquote>
  387. nrow = 2<br>
  388. ncolumn = 3<br>
  389. result[0] = "employee_name"<br>
  390. result[1] = "login"<br>
  391. result[2] = "host"<br>
  392. result[3] = "dummy"<br>
  393. result[4] = "No such user"<br>
  394. result[5] = 0<br>
  395. result[6] = "D. Richard Hipp"<br>
  396. result[7] = "drh"<br>
  397. result[8] = "zadok"
  398. </blockquote>
  399. <p>Notice that the "host" value for the "dummy" record is NULL so
  400. the result[] array contains a NULL pointer at that slot.</p>
  401. <p>If the result set of a query is empty, then by default
  402. <b>sqlite_get_table()</b> will set nrow to 0 and leave its
  403. result parameter is set to NULL. But if the EMPTY_RESULT_CALLBACKS
  404. pragma is ON then the result parameter is initialized to the names
  405. of the columns only. For example, consider this query which has
  406. an empty result set:</p>
  407. <blockquote>
  408. SELECT employee_name, login, host FROM users WHERE employee_name IS NULL;
  409. </blockquote>
  410. <p>
  411. The default behavior gives this results:
  412. </p>
  413. <blockquote>
  414. nrow = 0<br>
  415. ncolumn = 0<br>
  416. result = 0<br>
  417. </blockquote>
  418. <p>
  419. But if the EMPTY_RESULT_CALLBACKS pragma is ON, then the following
  420. is returned:
  421. </p>
  422. <blockquote>
  423. nrow = 0<br>
  424. ncolumn = 3<br>
  425. result[0] = "employee_name"<br>
  426. result[1] = "login"<br>
  427. result[2] = "host"<br>
  428. </blockquote>
  429. <p>Memory to hold the information returned by <b>sqlite_get_table()</b>
  430. is obtained from malloc(). But the calling function should not try
  431. to free this information directly. Instead, pass the complete table
  432. to <b>sqlite_free_table()</b> when the table is no longer needed.
  433. It is safe to call <b>sqlite_free_table()</b> with a NULL pointer such
  434. as would be returned if the result set is empty.</p>
  435. <p>The <b>sqlite_get_table()</b> routine returns the same integer
  436. result code as <b>sqlite_exec()</b>.</p>
  437. <h2>Interrupting an SQLite operation</h2>
  438. <p>The <b>sqlite_interrupt()</b> function can be called from a
  439. different thread or from a signal handler to cause the current database
  440. operation to exit at its first opportunity. When this happens,
  441. the <b>sqlite_exec()</b> routine (or the equivalent) that started
  442. the database operation will return SQLITE_INTERRUPT.</p>
  443. <h2>Testing for a complete SQL statement</h2>
  444. <p>The next interface routine to SQLite is a convenience function used
  445. to test whether or not a string forms a complete SQL statement.
  446. If the <b>sqlite_complete()</b> function returns true when its input
  447. is a string, then the argument forms a complete SQL statement.
  448. There are no guarantees that the syntax of that statement is correct,
  449. but we at least know the statement is complete. If <b>sqlite_complete()</b>
  450. returns false, then more text is required to complete the SQL statement.</p>
  451. <p>For the purpose of the <b>sqlite_complete()</b> function, an SQL
  452. statement is complete if it ends in a semicolon.</p>
  453. <p>The <b>sqlite</b> command-line utility uses the <b>sqlite_complete()</b>
  454. function to know when it needs to call <b>sqlite_exec()</b>. After each
  455. line of input is received, <b>sqlite</b> calls <b>sqlite_complete()</b>
  456. on all input in its buffer. If <b>sqlite_complete()</b> returns true,
  457. then <b>sqlite_exec()</b> is called and the input buffer is reset. If
  458. <b>sqlite_complete()</b> returns false, then the prompt is changed to
  459. the continuation prompt and another line of text is read and added to
  460. the input buffer.</p>
  461. <h2>Library version string</h2>
  462. <p>The SQLite library exports the string constant named
  463. <b>sqlite_version</b> which contains the version number of the
  464. library. The header file contains a macro SQLITE_VERSION
  465. with the same information. If desired, a program can compare
  466. the SQLITE_VERSION macro against the <b>sqlite_version</b>
  467. string constant to verify that the version number of the
  468. header file and the library match.</p>
  469. <h2>Library character encoding</h2>
  470. <p>By default, SQLite assumes that all data uses a fixed-size
  471. 8-bit character (iso8859). But if you give the --enable-utf8 option
  472. to the configure script, then the library assumes UTF-8 variable
  473. sized characters. This makes a difference for the LIKE and GLOB
  474. operators and the LENGTH() and SUBSTR() functions. The static
  475. string <b>sqlite_encoding</b> will be set to either "UTF-8" or
  476. "iso8859" to indicate how the library was compiled. In addition,
  477. the <b>sqlite.h</b> header file will define one of the
  478. macros <b>SQLITE_UTF8</b> or <b>SQLITE_ISO8859</b>, as appropriate.</p>
  479. <p>Note that the character encoding mechanism used by SQLite cannot
  480. be changed at run-time. This is a compile-time option only. The
  481. <b>sqlite_encoding</b> character string just tells you how the library
  482. was compiled.</p>
  483. <h2>Changing the library's response to locked files</h2>
  484. <p>The <b>sqlite_busy_handler()</b> procedure can be used to register
  485. a busy callback with an open SQLite database. The busy callback will
  486. be invoked whenever SQLite tries to access a database that is locked.
  487. The callback will typically do some other useful work, or perhaps sleep,
  488. in order to give the lock a chance to clear. If the callback returns
  489. non-zero, then SQLite tries again to access the database and the cycle
  490. repeats. If the callback returns zero, then SQLite aborts the current
  491. operation and returns SQLITE_BUSY.</p>
  492. <p>The arguments to <b>sqlite_busy_handler()</b> are the opaque
  493. structure returned from <b>sqlite_open()</b>, a pointer to the busy
  494. callback function, and a generic pointer that will be passed as
  495. the first argument to the busy callback. When SQLite invokes the
  496. busy callback, it sends it three arguments: the generic pointer
  497. that was passed in as the third argument to <b>sqlite_busy_handler</b>,
  498. the name of the database table or index that the library is trying
  499. to access, and the number of times that the library has attempted to
  500. access the database table or index.</p>
  501. <p>For the common case where we want the busy callback to sleep,
  502. the SQLite library provides a convenience routine <b>sqlite_busy_timeout()</b>.
  503. The first argument to <b>sqlite_busy_timeout()</b> is a pointer to
  504. an open SQLite database and the second argument is a number of milliseconds.
  505. After <b>sqlite_busy_timeout()</b> has been executed, the SQLite library
  506. will wait for the lock to clear for at least the number of milliseconds
  507. specified before it returns SQLITE_BUSY. Specifying zero milliseconds for
  508. the timeout restores the default behavior.</p>
  509. <h2>Using the <tt>_printf()</tt> wrapper functions</h2>
  510. <p>The four utility functions</p>
  511. <p>
  512. <ul>
  513. <li><b>sqlite_exec_printf()</b></li>
  514. <li><b>sqlite_exec_vprintf()</b></li>
  515. <li><b>sqlite_get_table_printf()</b></li>
  516. <li><b>sqlite_get_table_vprintf()</b></li>
  517. </ul>
  518. </p>
  519. <p>implement the same query functionality as <b>sqlite_exec()</b>
  520. and <b>sqlite_get_table()</b>. But instead of taking a complete
  521. SQL statement as their second argument, the four <b>_printf</b>
  522. routines take a printf-style format string. The SQL statement to
  523. be executed is generated from this format string and from whatever
  524. additional arguments are attached to the end of the function call.</p>
  525. <p>There are two advantages to using the SQLite printf
  526. functions instead of <b>sprintf()</b>. First of all, with the
  527. SQLite printf routines, there is never a danger of overflowing a
  528. static buffer as there is with <b>sprintf()</b>. The SQLite
  529. printf routines automatically allocate (and later free)
  530. as much memory as is
  531. necessary to hold the SQL statements generated.</p>
  532. <p>The second advantage the SQLite printf routines have over
  533. <b>sprintf()</b> are two new formatting options specifically designed
  534. to support string literals in SQL. Within the format string,
  535. the %q formatting option works very much like %s in that it
  536. reads a null-terminated string from the argument list and inserts
  537. it into the result. But %q translates the inserted string by
  538. making two copies of every single-quote (') character in the
  539. substituted string. This has the effect of escaping the end-of-string
  540. meaning of single-quote within a string literal. The %Q formatting
  541. option works similar; it translates the single-quotes like %q and
  542. additionally encloses the resulting string in single-quotes.
  543. If the argument for the %Q formatting options is a NULL pointer,
  544. the resulting string is NULL without single quotes.
  545. </p>
  546. <p>Consider an example. Suppose you are trying to insert a string
  547. value into a database table where the string value was obtained from
  548. user input. Suppose the string to be inserted is stored in a variable
  549. named zString. The code to do the insertion might look like this:</p>
  550. <blockquote><pre>
  551. sqlite_exec_printf(db,
  552. "INSERT INTO table1 VALUES('%s')",
  553. 0, 0, 0, zString);
  554. </pre></blockquote>
  555. <p>If the zString variable holds text like "Hello", then this statement
  556. will work just fine. But suppose the user enters a string like
  557. "Hi y'all!". The SQL statement generated reads as follows:
  558. <blockquote><pre>
  559. INSERT INTO table1 VALUES('Hi y'all')
  560. </pre></blockquote>
  561. <p>This is not valid SQL because of the apostrophy in the word "y'all".
  562. But if the %q formatting option is used instead of %s, like this:</p>
  563. <blockquote><pre>
  564. sqlite_exec_printf(db,
  565. "INSERT INTO table1 VALUES('%q')",
  566. 0, 0, 0, zString);
  567. </pre></blockquote>
  568. <p>Then the generated SQL will look like the following:</p>
  569. <blockquote><pre>
  570. INSERT INTO table1 VALUES('Hi y''all')
  571. </pre></blockquote>
  572. <p>Here the apostrophy has been escaped and the SQL statement is well-formed.
  573. When generating SQL on-the-fly from data that might contain a
  574. single-quote character ('), it is always a good idea to use the
  575. SQLite printf routines and the %q formatting option instead of <b>sprintf</b>.
  576. </p>
  577. <p>If the %Q formatting option is used instead of %q, like this:</p>
  578. <blockquote><pre>
  579. sqlite_exec_printf(db,
  580. "INSERT INTO table1 VALUES(%Q)",
  581. 0, 0, 0, zString);
  582. </pre></blockquote>
  583. <p>Then the generated SQL will look like the following:</p>
  584. <blockquote><pre>
  585. INSERT INTO table1 VALUES('Hi y''all')
  586. </pre></blockquote>
  587. <p>If the value of the zString variable is NULL, the generated SQL
  588. will look like the following:</p>
  589. <blockquote><pre>
  590. INSERT INTO table1 VALUES(NULL)
  591. </pre></blockquote>
  592. <h2>Adding New SQL Functions</h2>
  593. <p>Beginning with version 2.4.0, SQLite allows the SQL language to be
  594. extended with new functions implemented as C code. The following interface
  595. is used:
  596. </p>
  597. <blockquote><pre>
  598. typedef struct sqlite_func sqlite_func;
  599. int sqlite_create_function(
  600. sqlite *db,
  601. const char *zName,
  602. int nArg,
  603. void (*xFunc)(sqlite_func*,int,const char**),
  604. void *pUserData
  605. );
  606. int sqlite_create_aggregate(
  607. sqlite *db,
  608. const char *zName,
  609. int nArg,
  610. void (*xStep)(sqlite_func*,int,const char**),
  611. void (*xFinalize)(sqlite_func*),
  612. void *pUserData
  613. );
  614. char *sqlite_set_result_string(sqlite_func*,const char*,int);
  615. void sqlite_set_result_int(sqlite_func*,int);
  616. void sqlite_set_result_double(sqlite_func*,double);
  617. void sqlite_set_result_error(sqlite_func*,const char*,int);
  618. void *sqlite_user_data(sqlite_func*);
  619. void *sqlite_aggregate_context(sqlite_func*, int nBytes);
  620. int sqlite_aggregate_count(sqlite_func*);
  621. </pre></blockquote>
  622. <p>
  623. The <b>sqlite_create_function()</b> interface is used to create
  624. regular functions and <b>sqlite_create_aggregate()</b> is used to
  625. create new aggregate functions. In both cases, the <b>db</b>
  626. parameter is an open SQLite database on which the functions should
  627. be registered, <b>zName</b> is the name of the new function,
  628. <b>nArg</b> is the number of arguments, and <b>pUserData</b> is
  629. a pointer which is passed through unchanged to the C implementation
  630. of the function.
  631. </p>
  632. <p>
  633. For regular functions, the <b>xFunc</b> callback is invoked once
  634. for each function call. The implementation of xFunc should call
  635. one of the <b>sqlite_set_result_...</b> interfaces to return its
  636. result. The <b>sqlite_user_data()</b> routine can be used to
  637. retrieve the <b>pUserData</b> pointer that was passed in when the
  638. function was registered.
  639. </p>
  640. <p>
  641. For aggregate functions, the <b>xStep</b> callback is invoked once
  642. for each row in the result and then <b>xFinalize</b> is invoked at the
  643. end to compute a final answer. The xStep routine can use the
  644. <b>sqlite_aggregate_context()</b> interface to allocate memory that
  645. will be unique to that particular instance of the SQL function.
  646. This memory will be automatically deleted after xFinalize is called.
  647. The <b>sqlite_aggregate_count()</b> routine can be used to find out
  648. how many rows of data were passed to the aggregate. The xFinalize
  649. callback should invoke one of the <b>sqlite_set_result_...</b>
  650. interfaces to set the final result of the aggregate.
  651. </p>
  652. <p>
  653. SQLite now implements all of its built-in functions using this
  654. interface. For additional information and examples on how to create
  655. new SQL functions, review the SQLite source code in the file
  656. <b>func.c</b>.
  657. </p>
  658. <h2>Usage Examples</h2>
  659. <p>For examples of how the SQLite C/C++ interface can be used,
  660. refer to the source code for the <b>sqlite</b> program in the
  661. file <b>src/shell.c</b> of the source tree.
  662. Additional information about sqlite is available at
  663. <a href="sqlite.html">sqlite.html</a>.
  664. See also the sources to the Tcl interface for SQLite in
  665. the source file <b>src/tclsqlite.c</b>.</p>
  666. }
  667. puts {
  668. <p><hr /></p>
  669. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  670. Back to the SQLite Home Page</a>
  671. </p>
  672. </body></html>}