blob: 4bf2fdd4de98aec06313fcf62f42cd4188c831a1 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This header file defines the interface that the SQLite library
drh75897232000-05-29 14:26:00 +000013** presents to client programs.
14**
drh382c0242001-10-06 16:33:02 +000015** @(#) $Id: sqlite.h.in,v 1.21 2001/10/06 16:33:03 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#ifndef _SQLITE_H_
18#define _SQLITE_H_
drha18c5682000-10-08 22:20:57 +000019#include <stdarg.h> /* Needed for the definition of va_list */
drh75897232000-05-29 14:26:00 +000020
21/*
drhb217a572000-08-22 13:40:18 +000022** The version of the SQLite library.
drh303aaa72000-08-17 10:22:34 +000023*/
drhb217a572000-08-22 13:40:18 +000024#define SQLITE_VERSION "--VERS--"
25
26/*
drh382c0242001-10-06 16:33:02 +000027** Make sure we can call this stuff from C++.
28*/
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/*
drhb217a572000-08-22 13:40:18 +000034** The version string is also compiled into the library so that a program
35** can check to make sure that the lib*.a file and the *.h file are from
36** the same version.
37*/
38extern const char sqlite_version[];
drh303aaa72000-08-17 10:22:34 +000039
40/*
drh297ecf12001-04-05 15:57:13 +000041** The SQLITE_UTF8 macro is defined if the library expects to see
42** UTF-8 encoded data. The SQLITE_ISO8859 macro is defined if the
43** iso8859 encoded should be used.
44*/
45#define SQLITE_--ENCODING-- 1
46
47/*
48** The following constant holds one of two strings, "UTF-8" or "iso8859",
49** depending on which character encoding the SQLite library expects to
50** see. The character encoding makes a difference for the LIKE and GLOB
51** operators and for the LENGTH() and SUBSTR() functions.
52*/
drhfbc3eab2001-04-06 16:13:42 +000053extern const char sqlite_encoding[];
drh297ecf12001-04-05 15:57:13 +000054
55/*
drh75897232000-05-29 14:26:00 +000056** Each open sqlite database is represented by an instance of the
57** following opaque structure.
58*/
59typedef struct sqlite sqlite;
60
61/*
62** A function to open a new sqlite database.
63**
64** If the database does not exist and mode indicates write
65** permission, then a new database is created. If the database
66** does not exist and mode does not indicate write permission,
67** then the open fails, an error message generated (if errmsg!=0)
68** and the function returns 0.
69**
70** If mode does not indicates user write permission, then the
71** database is opened read-only.
72**
73** The Truth: As currently implemented, all databases are opened
74** for writing all the time. Maybe someday we will provide the
75** ability to open a database readonly. The mode parameters is
76** provide in anticipation of that enhancement.
77*/
78sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
79
80/*
81** A function to close the database.
82**
83** Call this function with a pointer to a structure that was previously
84** returned from sqlite_open() and the corresponding database will by closed.
85*/
86void sqlite_close(sqlite *);
87
88/*
89** The type for a callback function.
90*/
91typedef int (*sqlite_callback)(void*,int,char**, char**);
92
93/*
94** A function to executes one or more statements of SQL.
95**
96** If one or more of the SQL statements are queries, then
97** the callback function specified by the 3rd parameter is
98** invoked once for each row of the query result. This callback
99** should normally return 0. If the callback returns a non-zero
100** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +0000101** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +0000102**
103** The 4th parameter is an arbitrary pointer that is passed
104** to the callback function as its first parameter.
105**
106** The 2nd parameter to the callback function is the number of
drhb19a2bc2001-09-16 00:13:26 +0000107** columns in the query result. The 3rd parameter to the callback
108** is an array of strings holding the values for each column.
109** The 4th parameter to the callback is an array of strings holding
110** the names of each column.
drh75897232000-05-29 14:26:00 +0000111**
112** The callback function may be NULL, even for queries. A NULL
113** callback is not an error. It just means that no callback
114** will be invoked.
115**
116** If an error occurs while parsing or evaluating the SQL (but
117** not while executing the callback) then an appropriate error
118** message is written into memory obtained from malloc() and
drhb19a2bc2001-09-16 00:13:26 +0000119** *errmsg is made to point to that message. The calling function
120** is responsible for freeing the memory that holds the error
121** message. If errmsg==NULL, then no error message is ever written.
122**
123** The return value is is SQLITE_OK if there are no errors and
124** some other return code if there is an error. The particular
125** return value depends on the type of error.
drh58b95762000-06-02 01:17:37 +0000126**
127** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000128** locked or busy, then this function returns SQLITE_BUSY. (This
129** behavior can be modified somewhat using the sqlite_busy_handler()
drhb19a2bc2001-09-16 00:13:26 +0000130** and sqlite_busy_timeout() functions below.)
drh75897232000-05-29 14:26:00 +0000131*/
132int sqlite_exec(
133 sqlite*, /* An open database */
134 char *sql, /* SQL to be executed */
135 sqlite_callback, /* Callback function */
136 void *, /* 1st argument to callback function */
137 char **errmsg /* Error msg written here */
138);
139
drh58b95762000-06-02 01:17:37 +0000140/*
drh98699b52000-10-09 12:57:00 +0000141** Return values for sqlite_exec()
drh58b95762000-06-02 01:17:37 +0000142*/
drh717e6402001-09-27 03:22:32 +0000143#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
164/* If the parameter to this routine is one of the return value constants
165** defined above, then this routine returns a constant text string which
166** descripts (in English) the meaning of the return value.
167*/
168const char *sqliteErrStr(int);
drh4c504392000-10-16 22:06:40 +0000169
170/* This function causes any pending database operation to abort and
171** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000172** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000173** or Ctrl-C where the user wants a long query operation to halt
174** immediately.
175*/
176void sqlite_interrupt(sqlite*);
177
drheec553b2000-06-02 01:51:20 +0000178
drh75897232000-05-29 14:26:00 +0000179/* This function returns true if the given input string comprises
180** one or more complete SQL statements.
181**
182** The algorithm is simple. If the last token other than spaces
183** and comments is a semicolon, then return true. otherwise return
184** false.
185*/
186int sqlite_complete(const char *sql);
187
drh2dfbbca2000-07-28 14:32:48 +0000188/*
189** This routine identifies a callback function that is invoked
190** whenever an attempt is made to open a database table that is
191** currently locked by another process or thread. If the busy callback
192** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
193** it finds a locked table. If the busy callback is not NULL, then
194** sqlite_exec() invokes the callback with three arguments. The
195** second argument is the name of the locked table and the third
196** argument is the number of times the table has been busy. If the
197** busy callback returns 0, then sqlite_exec() immediately returns
198** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
199** tries to open the table again and the cycle repeats.
200**
201** The default busy callback is NULL.
202**
203** Sqlite is re-entrant, so the busy handler may start a new query.
204** (It is not clear why anyone would every want to do this, but it
205** is allowed, in theory.) But the busy handler may not close the
206** database. Closing the database from a busy handler will delete
207** data structures out from under the executing query and will
208** probably result in a coredump.
209*/
210void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
211
212/*
213** This routine sets a busy handler that sleeps for a while when a
214** table is locked. The handler will sleep multiple times until
215** at least "ms" milleseconds of sleeping have been done. After
216** "ms" milleseconds of sleeping, the handler returns 0 which
217** causes sqlite_exec() to return SQLITE_BUSY.
218**
219** Calling this routine with an argument less than or equal to zero
220** turns off all busy handlers.
221*/
222void sqlite_busy_timeout(sqlite*, int ms);
223
drhe3710332000-09-29 13:30:53 +0000224/*
225** This next routine is really just a wrapper around sqlite_exec().
226** Instead of invoking a user-supplied callback for each row of the
227** result, this routine remembers each row of the result in memory
228** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000229** query has finished.
230**
231** As an example, suppose the query result where this table:
232**
233** Name | Age
234** -----------------------
235** Alice | 43
236** Bob | 28
237** Cindy | 21
238**
239** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000240** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000241**
242** azResult[0] = "Name";
243** azResult[1] = "Age";
244** azResult[2] = "Alice";
245** azResult[3] = "43";
246** azResult[4] = "Bob";
247** azResult[5] = "28";
248** azResult[6] = "Cindy";
249** azResult[7] = "21";
250**
251** Notice that there is an extra row of data containing the column
252** headers. But the *nrow return value is still 3. *ncolumn is
253** set to 2. In general, the number of values inserted into azResult
254** will be ((*nrow) + 1)*(*ncolumn).
255**
256** After the calling function has finished using the result, it should
257** pass the result data pointer to sqlite_free_table() in order to
258** release the memory that was malloc-ed. Because of the way the
259** malloc() happens, the calling function must not try to call
260** malloc() directly. Only sqlite_free_table() is able to release
261** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000262**
263** The return value of this routine is the same as from sqlite_exec().
264*/
265int sqlite_get_table(
266 sqlite*, /* An open database */
267 char *sql, /* SQL to be executed */
268 char ***resultp, /* Result written to a char *[] that this points to */
269 int *nrow, /* Number of result rows written here */
270 int *ncolumn, /* Number of result columns written here */
271 char **errmsg /* Error msg written here */
272);
273
274/*
275** Call this routine to free the memory that sqlite_get_table() allocated.
276*/
277void sqlite_free_table(char **result);
278
drha18c5682000-10-08 22:20:57 +0000279/*
280** The following routines are wrappers around sqlite_exec() and
drh98699b52000-10-09 12:57:00 +0000281** sqlite_get_table(). The only difference between the routines that
drha18c5682000-10-08 22:20:57 +0000282** follow and the originals is that the second argument to the
283** routines that follow is really a printf()-style format
284** string describing the SQL to be executed. Arguments to the format
285** string appear at the end of the argument list.
286**
287** All of the usual printf formatting options apply. In addition, there
288** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000289** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000290** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000291** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000292** the string.
293**
294** For example, so some string variable contains text as follows:
295**
296** char *zText = "It's a happy day!";
297**
298** We can use this text in an SQL statement as follows:
299**
300** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
301** callback1, 0, 0, zText);
302**
303** Because the %q format string is used, the '\'' character in zText
304** is escaped and the SQL generated is as follows:
305**
306** INSERT INTO table1 VALUES('It''s a happy day!')
307**
308** This is correct. Had we used %s instead of %q, the generated SQL
309** would have looked like this:
310**
311** INSERT INTO table1 VALUES('It's a happy day!');
312**
313** This second example is an SQL syntax error. As a general rule you
314** should always use %q instead of %s when inserting text into a string
315** literal.
316*/
317int sqlite_exec_printf(
318 sqlite*, /* An open database */
319 char *sqlFormat, /* printf-style format string for the SQL */
320 sqlite_callback, /* Callback function */
321 void *, /* 1st argument to callback function */
322 char **errmsg, /* Error msg written here */
323 ... /* Arguments to the format string. */
324);
325int sqlite_exec_vprintf(
326 sqlite*, /* An open database */
327 char *sqlFormat, /* printf-style format string for the SQL */
328 sqlite_callback, /* Callback function */
329 void *, /* 1st argument to callback function */
330 char **errmsg, /* Error msg written here */
331 va_list ap /* Arguments to the format string. */
332);
333int sqlite_get_table_printf(
334 sqlite*, /* An open database */
335 char *sqlFormat, /* printf-style format string for the SQL */
336 char ***resultp, /* Result written to a char *[] that this points to */
337 int *nrow, /* Number of result rows written here */
338 int *ncolumn, /* Number of result columns written here */
339 char **errmsg, /* Error msg written here */
340 ... /* Arguments to the format string */
341);
342int sqlite_get_table_vprintf(
343 sqlite*, /* An open database */
344 char *sqlFormat, /* printf-style format string for the SQL */
345 char ***resultp, /* Result written to a char *[] that this points to */
346 int *nrow, /* Number of result rows written here */
347 int *ncolumn, /* Number of result columns written here */
348 char **errmsg, /* Error msg written here */
349 va_list ap /* Arguments to the format string */
350);
351
drh382c0242001-10-06 16:33:02 +0000352#ifdef __cplusplus
353} /* End of the 'extern "C"' block */
354#endif
355
drh75897232000-05-29 14:26:00 +0000356#endif /* _SQLITE_H_ */