xref: /netbsd-src/external/public-domain/sqlite/man/sqlite3_prepare.3 (revision 2dd295436a0082eb4f8d294f4aa73c223413d0f2)
1.Dd December 19, 2018
2.Dt SQLITE3_PREPARE 3
3.Os
4.Sh NAME
5.Nm sqlite3_prepare ,
6.Nm sqlite3_prepare_v2 ,
7.Nm sqlite3_prepare_v3 ,
8.Nm sqlite3_prepare16 ,
9.Nm sqlite3_prepare16_v2 ,
10.Nm sqlite3_prepare16_v3
11.Nd Compiling An SQL Statement
12.Sh SYNOPSIS
13.Ft int
14.Fo sqlite3_prepare
15.Fa "sqlite3 *db"
16.Fa "const char *zSql"
17.Fa "int nByte"
18.Fa "sqlite3_stmt **ppStmt"
19.Fa "const char **pzTail     "
20.Fc
21.Ft int
22.Fo sqlite3_prepare_v2
23.Fa "sqlite3 *db"
24.Fa "const char *zSql"
25.Fa "int nByte"
26.Fa "sqlite3_stmt **ppStmt"
27.Fa "const char **pzTail     "
28.Fc
29.Ft int
30.Fo sqlite3_prepare_v3
31.Fa "sqlite3 *db"
32.Fa "const char *zSql"
33.Fa "int nByte"
34.Fa "unsigned int prepFlags"
35.Fa "sqlite3_stmt **ppStmt"
36.Fa "const char **pzTail     "
37.Fc
38.Ft int
39.Fo sqlite3_prepare16
40.Fa "sqlite3 *db"
41.Fa "const void *zSql"
42.Fa "int nByte"
43.Fa "sqlite3_stmt **ppStmt"
44.Fa "const void **pzTail     "
45.Fc
46.Ft int
47.Fo sqlite3_prepare16_v2
48.Fa "sqlite3 *db"
49.Fa "const void *zSql"
50.Fa "int nByte"
51.Fa "sqlite3_stmt **ppStmt"
52.Fa "const void **pzTail     "
53.Fc
54.Ft int
55.Fo sqlite3_prepare16_v3
56.Fa "sqlite3 *db"
57.Fa "const void *zSql"
58.Fa "int nByte"
59.Fa "unsigned int prepFlags"
60.Fa "sqlite3_stmt **ppStmt"
61.Fa "const void **pzTail     "
62.Fc
63.Sh DESCRIPTION
64To execute an SQL statement, it must first be compiled into a byte-code
65program using one of these routines.
66Or, in other words, these routines are constructors for the prepared statement
67object.
68.Pp
69The preferred routine to use is sqlite3_prepare_v2().
70The sqlite3_prepare() interface is legacy and should
71be avoided.
72sqlite3_prepare_v3() has an extra "prepFlags" option
73that is used for special purposes.
74.Pp
75The use of the UTF-8 interfaces is preferred, as SQLite currently does
76all parsing using UTF-8.
77The UTF-16 interfaces are provided as a convenience.
78The UTF-16 interfaces work by converting the input text into UTF-8,
79then invoking the corresponding UTF-8 interface.
80.Pp
81The first argument, "db", is a database connection
82obtained from a prior successful call to sqlite3_open(),
83sqlite3_open_v2() or sqlite3_open16().
84The database connection must not have been closed.
85.Pp
86The second argument, "zSql", is the statement to be compiled, encoded
87as either UTF-8 or UTF-16.
88The sqlite3_prepare(), sqlite3_prepare_v2(), and sqlite3_prepare_v3()
89interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
90and sqlite3_prepare16_v3() use UTF-16.
91.Pp
92If the nByte argument is negative, then zSql is read up to the first
93zero terminator.
94If nByte is positive, then it is the number of bytes read from zSql.
95If nByte is zero, then no prepared statement is generated.
96If the caller knows that the supplied string is nul-terminated, then
97there is a small performance advantage to passing an nByte parameter
98that is the number of bytes in the input string <i>including</i> the
99nul-terminator.
100.Pp
101If pzTail is not NULL then *pzTail is made to point to the first byte
102past the end of the first SQL statement in zSql.
103These routines only compile the first statement in zSql, so *pzTail
104is left pointing to what remains uncompiled.
105.Pp
106*ppStmt is left pointing to a compiled prepared statement
107that can be executed using sqlite3_step().
108If there is an error, *ppStmt is set to NULL.
109If the input text contains no SQL (if the input is an empty string
110or a comment) then *ppStmt is set to NULL.
111The calling procedure is responsible for deleting the compiled SQL
112statement using sqlite3_finalize() after it has finished
113with it.
114ppStmt may not be NULL.
115.Pp
116On success, the sqlite3_prepare() family of routines return SQLITE_OK;
117otherwise an error code is returned.
118.Pp
119The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
120and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
121The older interfaces (sqlite3_prepare() and sqlite3_prepare16()) are
122retained for backwards compatibility, but their use is discouraged.
123In the "vX" interfaces, the prepared statement that is returned (the
124sqlite3_stmt object) contains a copy of the original SQL
125text.
126This causes the sqlite3_step() interface to behave differently
127in three ways:
128.Bl -enum
129.It
130If the database schema changes, instead of returning SQLITE_SCHEMA
131as it always used to do, sqlite3_step() will automatically
132recompile the SQL statement and try to run it again.
133As many as SQLITE_MAX_SCHEMA_RETRY retries will
134occur before sqlite3_step() gives up and returns an error.
135.It
136When an error occurs, sqlite3_step() will return one
137of the detailed error codes or extended error codes.
138The legacy behavior was that sqlite3_step() would only
139return a generic SQLITE_ERROR result code and the application
140would have to make a second call to sqlite3_reset()
141in order to find the underlying cause of the problem.
142With the "v2" prepare interfaces, the underlying reason for the error
143is returned immediately.
144.It
145If the specific value bound to  host parameter in the
146WHERE clause might influence the choice of query plan for a statement,
147then the statement will be automatically recompiled, as if there had
148been a schema change, on the first  sqlite3_step() call
149following any change to the  bindings of that parameter.
150The specific value of WHERE-clause parameter might influence
151the choice of query plan if the parameter is the left-hand side of
152a LIKE or GLOB operator or if the parameter is compared to
153an indexed column and the SQLITE_ENABLE_STAT3 compile-time
154option is enabled.
155.El
156.Pp
157<p> sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in
158having the extra prepFlags parameter, which is a bit array consisting
159of zero or more of the SQLITE_PREPARE_* flags.
160The sqlite3_prepare_v2() interface works exactly the same as sqlite3_prepare_v3()
161with a zero prepFlags parameter.
162.Sh SEE ALSO
163.Xr sqlite3 3 ,
164.Xr sqlite3_stmt 3 ,
165.Xr sqlite3_bind_blob 3 ,
166.Xr sqlite3_finalize 3 ,
167.Xr sqlite3_open 3 ,
168.Xr sqlite3_prepare 3 ,
169.Xr sqlite3_reset 3 ,
170.Xr sqlite3_step 3 ,
171.Xr sqlite3_stmt 3 ,
172.Xr SQLITE_OK 3 ,
173.Xr SQLITE_PREPARE_PERSISTENT 3 ,
174.Xr SQLITE_OK 3
175