blob: c22b9cb3cfd58696640d21a38696eaa3d3d09485 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This header file defines the interface that the sqlite library
25** presents to client programs.
26**
drh092d0352001-09-15 13:15:12 +000027** @(#) $Id: sqlite.h.in,v 1.16 2001/09/15 13:15:13 drh Exp $
drh75897232000-05-29 14:26:00 +000028*/
29#ifndef _SQLITE_H_
30#define _SQLITE_H_
drha18c5682000-10-08 22:20:57 +000031#include <stdarg.h> /* Needed for the definition of va_list */
drh75897232000-05-29 14:26:00 +000032
33/*
drhb217a572000-08-22 13:40:18 +000034** The version of the SQLite library.
drh303aaa72000-08-17 10:22:34 +000035*/
drhb217a572000-08-22 13:40:18 +000036#define SQLITE_VERSION "--VERS--"
37
38/*
39** The version string is also compiled into the library so that a program
40** can check to make sure that the lib*.a file and the *.h file are from
41** the same version.
42*/
43extern const char sqlite_version[];
drh303aaa72000-08-17 10:22:34 +000044
45/*
drh297ecf12001-04-05 15:57:13 +000046** The SQLITE_UTF8 macro is defined if the library expects to see
47** UTF-8 encoded data. The SQLITE_ISO8859 macro is defined if the
48** iso8859 encoded should be used.
49*/
50#define SQLITE_--ENCODING-- 1
51
52/*
53** The following constant holds one of two strings, "UTF-8" or "iso8859",
54** depending on which character encoding the SQLite library expects to
55** see. The character encoding makes a difference for the LIKE and GLOB
56** operators and for the LENGTH() and SUBSTR() functions.
57*/
drhfbc3eab2001-04-06 16:13:42 +000058extern const char sqlite_encoding[];
drh297ecf12001-04-05 15:57:13 +000059
60/*
drh75897232000-05-29 14:26:00 +000061** Each open sqlite database is represented by an instance of the
62** following opaque structure.
63*/
64typedef struct sqlite sqlite;
65
66/*
67** A function to open a new sqlite database.
68**
69** If the database does not exist and mode indicates write
70** permission, then a new database is created. If the database
71** does not exist and mode does not indicate write permission,
72** then the open fails, an error message generated (if errmsg!=0)
73** and the function returns 0.
74**
75** If mode does not indicates user write permission, then the
76** database is opened read-only.
77**
78** The Truth: As currently implemented, all databases are opened
79** for writing all the time. Maybe someday we will provide the
80** ability to open a database readonly. The mode parameters is
81** provide in anticipation of that enhancement.
82*/
83sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
84
85/*
86** A function to close the database.
87**
88** Call this function with a pointer to a structure that was previously
89** returned from sqlite_open() and the corresponding database will by closed.
90*/
91void sqlite_close(sqlite *);
92
93/*
94** The type for a callback function.
95*/
96typedef int (*sqlite_callback)(void*,int,char**, char**);
97
98/*
99** A function to executes one or more statements of SQL.
100**
101** If one or more of the SQL statements are queries, then
102** the callback function specified by the 3rd parameter is
103** invoked once for each row of the query result. This callback
104** should normally return 0. If the callback returns a non-zero
105** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +0000106** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +0000107**
108** The 4th parameter is an arbitrary pointer that is passed
109** to the callback function as its first parameter.
110**
111** The 2nd parameter to the callback function is the number of
112** columns in the query result. The 3rd parameter is an array
113** of string holding the values for each column. The 4th parameter
114** is an array of strings holding the names of each column.
115**
116** The callback function may be NULL, even for queries. A NULL
117** callback is not an error. It just means that no callback
118** will be invoked.
119**
120** If an error occurs while parsing or evaluating the SQL (but
121** not while executing the callback) then an appropriate error
122** message is written into memory obtained from malloc() and
123** *errmsg is made to point to that message. If errmsg==NULL,
124** then no error message is ever written. The return value is
drh2dfbbca2000-07-28 14:32:48 +0000125** SQLITE_ERROR if an error occurs. The calling function is
126** responsible for freeing the memory that holds the error
127** message.
drh58b95762000-06-02 01:17:37 +0000128**
129** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000130** locked or busy, then this function returns SQLITE_BUSY. (This
131** behavior can be modified somewhat using the sqlite_busy_handler()
132** and sqlite_busy_timeout() functions below.) If the query could
133** not be executed because a file is missing or has incorrect
134** permissions, this function returns SQLITE_ERROR.
drh75897232000-05-29 14:26:00 +0000135*/
136int sqlite_exec(
137 sqlite*, /* An open database */
138 char *sql, /* SQL to be executed */
139 sqlite_callback, /* Callback function */
140 void *, /* 1st argument to callback function */
141 char **errmsg /* Error msg written here */
142);
143
drh58b95762000-06-02 01:17:37 +0000144/*
drh98699b52000-10-09 12:57:00 +0000145** Return values for sqlite_exec()
drh58b95762000-06-02 01:17:37 +0000146*/
147#define SQLITE_OK 0 /* Successful result */
drh4c504392000-10-16 22:06:40 +0000148#define SQLITE_ERROR 1 /* SQL error or missing database */
149#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
drheec553b2000-06-02 01:51:20 +0000150#define SQLITE_PERM 3 /* Access permission denied */
151#define SQLITE_ABORT 4 /* Callback routine requested an abort */
152#define SQLITE_BUSY 5 /* One or more database files are locked */
153#define SQLITE_NOMEM 6 /* A malloc() failed */
154#define SQLITE_READONLY 7 /* Attempt to write a readonly database */
drh4c504392000-10-16 22:06:40 +0000155#define SQLITE_INTERRUPT 8 /* Operation terminated by sqlite_interrupt() */
drh345fda32001-01-15 22:51:08 +0000156#define SQLITE_IOERR 9 /* Disk full or other I/O error */
drh41a2b482001-01-20 19:52:49 +0000157#define SQLITE_CORRUPT 10 /* The database disk image is malformed */
158#define SQLITE_NOTFOUND 11 /* Table or record not found */
159#define SQLITE_FULL 12 /* Insertion failed because database is full */
drh960e8c62001-04-03 16:53:21 +0000160#define SQLITE_CANTOPEN 13 /* Unable to open the database file */
161#define SQLITE_PROTOCOL 14 /* Database lock protocol error */
drh5e00f6c2001-09-13 13:46:56 +0000162#define SQLITE_EMPTY 15 /* Database table is empty */
drh50e5dad2001-09-15 00:57:28 +0000163#define SQLITE_SCHEMA 16 /* The database schema changed */
drh092d0352001-09-15 13:15:12 +0000164#define SQLITE_TOOBIG 17 /* Too much data for one row of a table */
drh4c504392000-10-16 22:06:40 +0000165
166/* This function causes any pending database operation to abort and
167** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000168** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000169** or Ctrl-C where the user wants a long query operation to halt
170** immediately.
171*/
172void sqlite_interrupt(sqlite*);
173
drheec553b2000-06-02 01:51:20 +0000174
drh75897232000-05-29 14:26:00 +0000175/* This function returns true if the given input string comprises
176** one or more complete SQL statements.
177**
178** The algorithm is simple. If the last token other than spaces
179** and comments is a semicolon, then return true. otherwise return
180** false.
181*/
182int sqlite_complete(const char *sql);
183
drh2dfbbca2000-07-28 14:32:48 +0000184/*
185** This routine identifies a callback function that is invoked
186** whenever an attempt is made to open a database table that is
187** currently locked by another process or thread. If the busy callback
188** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
189** it finds a locked table. If the busy callback is not NULL, then
190** sqlite_exec() invokes the callback with three arguments. The
191** second argument is the name of the locked table and the third
192** argument is the number of times the table has been busy. If the
193** busy callback returns 0, then sqlite_exec() immediately returns
194** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
195** tries to open the table again and the cycle repeats.
196**
197** The default busy callback is NULL.
198**
199** Sqlite is re-entrant, so the busy handler may start a new query.
200** (It is not clear why anyone would every want to do this, but it
201** is allowed, in theory.) But the busy handler may not close the
202** database. Closing the database from a busy handler will delete
203** data structures out from under the executing query and will
204** probably result in a coredump.
205*/
206void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
207
208/*
209** This routine sets a busy handler that sleeps for a while when a
210** table is locked. The handler will sleep multiple times until
211** at least "ms" milleseconds of sleeping have been done. After
212** "ms" milleseconds of sleeping, the handler returns 0 which
213** causes sqlite_exec() to return SQLITE_BUSY.
214**
215** Calling this routine with an argument less than or equal to zero
216** turns off all busy handlers.
217*/
218void sqlite_busy_timeout(sqlite*, int ms);
219
drhe3710332000-09-29 13:30:53 +0000220/*
221** This next routine is really just a wrapper around sqlite_exec().
222** Instead of invoking a user-supplied callback for each row of the
223** result, this routine remembers each row of the result in memory
224** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000225** query has finished.
226**
227** As an example, suppose the query result where this table:
228**
229** Name | Age
230** -----------------------
231** Alice | 43
232** Bob | 28
233** Cindy | 21
234**
235** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000236** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000237**
238** azResult[0] = "Name";
239** azResult[1] = "Age";
240** azResult[2] = "Alice";
241** azResult[3] = "43";
242** azResult[4] = "Bob";
243** azResult[5] = "28";
244** azResult[6] = "Cindy";
245** azResult[7] = "21";
246**
247** Notice that there is an extra row of data containing the column
248** headers. But the *nrow return value is still 3. *ncolumn is
249** set to 2. In general, the number of values inserted into azResult
250** will be ((*nrow) + 1)*(*ncolumn).
251**
252** After the calling function has finished using the result, it should
253** pass the result data pointer to sqlite_free_table() in order to
254** release the memory that was malloc-ed. Because of the way the
255** malloc() happens, the calling function must not try to call
256** malloc() directly. Only sqlite_free_table() is able to release
257** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000258**
259** The return value of this routine is the same as from sqlite_exec().
260*/
261int sqlite_get_table(
262 sqlite*, /* An open database */
263 char *sql, /* SQL to be executed */
264 char ***resultp, /* Result written to a char *[] that this points to */
265 int *nrow, /* Number of result rows written here */
266 int *ncolumn, /* Number of result columns written here */
267 char **errmsg /* Error msg written here */
268);
269
270/*
271** Call this routine to free the memory that sqlite_get_table() allocated.
272*/
273void sqlite_free_table(char **result);
274
drha18c5682000-10-08 22:20:57 +0000275/*
276** The following routines are wrappers around sqlite_exec() and
drh98699b52000-10-09 12:57:00 +0000277** sqlite_get_table(). The only difference between the routines that
drha18c5682000-10-08 22:20:57 +0000278** follow and the originals is that the second argument to the
279** routines that follow is really a printf()-style format
280** string describing the SQL to be executed. Arguments to the format
281** string appear at the end of the argument list.
282**
283** All of the usual printf formatting options apply. In addition, there
284** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000285** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000286** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000287** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000288** the string.
289**
290** For example, so some string variable contains text as follows:
291**
292** char *zText = "It's a happy day!";
293**
294** We can use this text in an SQL statement as follows:
295**
296** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
297** callback1, 0, 0, zText);
298**
299** Because the %q format string is used, the '\'' character in zText
300** is escaped and the SQL generated is as follows:
301**
302** INSERT INTO table1 VALUES('It''s a happy day!')
303**
304** This is correct. Had we used %s instead of %q, the generated SQL
305** would have looked like this:
306**
307** INSERT INTO table1 VALUES('It's a happy day!');
308**
309** This second example is an SQL syntax error. As a general rule you
310** should always use %q instead of %s when inserting text into a string
311** literal.
312*/
313int sqlite_exec_printf(
314 sqlite*, /* An open database */
315 char *sqlFormat, /* printf-style format string for the SQL */
316 sqlite_callback, /* Callback function */
317 void *, /* 1st argument to callback function */
318 char **errmsg, /* Error msg written here */
319 ... /* Arguments to the format string. */
320);
321int sqlite_exec_vprintf(
322 sqlite*, /* An open database */
323 char *sqlFormat, /* printf-style format string for the SQL */
324 sqlite_callback, /* Callback function */
325 void *, /* 1st argument to callback function */
326 char **errmsg, /* Error msg written here */
327 va_list ap /* Arguments to the format string. */
328);
329int sqlite_get_table_printf(
330 sqlite*, /* An open database */
331 char *sqlFormat, /* printf-style format string for the SQL */
332 char ***resultp, /* Result written to a char *[] that this points to */
333 int *nrow, /* Number of result rows written here */
334 int *ncolumn, /* Number of result columns written here */
335 char **errmsg, /* Error msg written here */
336 ... /* Arguments to the format string */
337);
338int sqlite_get_table_vprintf(
339 sqlite*, /* An open database */
340 char *sqlFormat, /* printf-style format string for the SQL */
341 char ***resultp, /* Result written to a char *[] that this points to */
342 int *nrow, /* Number of result rows written here */
343 int *ncolumn, /* Number of result columns written here */
344 char **errmsg, /* Error msg written here */
345 va_list ap /* Arguments to the format string */
346);
347
drh75897232000-05-29 14:26:00 +0000348#endif /* _SQLITE_H_ */