描述
要执行SQL查询,必须首先使用其中一个准备函数将其编译成字节码程序。
C / C ++语法
int sqlite3_prepare16_v2( sqlite3 *db, const void *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail ) |
PB语法
FUNCTION sqlite3_prepare16_v2 ( _ BYVAL hDbc AS DWORD, _ BYREF wszSql AS WSTRINGZ, _ BYVAL nBytes AS LONG, _ BYREF ppStmt AS DWORD, _ BYREF pszTail AS DWORD _ ) AS LONG |
参数
pDb
[in]数据库连接句柄。必须是从sqlite3_opensqlite3_open16或sqlite3_open_v2获取的sqlite3对象指针。 数据库连接不能关闭。
wszSql
[in]要编译的语句,编码为UTF-16。
nBytes
[in]如果为nbytes参数小于零,则szSql读取到第一个零终止符。如果为nbytes为非负数,则为从szSql读取的最大字节数。当为nbytes为非负数时,szSql字符串以第一个“\\ 000”或“\\ u0000”字符或第nB字节结尾,以先到者为准。如果调用者知道提供的字符串是非终止的,那么通过传递等于输入字符串中包含Nul-Terminator字节的字节数的为nbytes参数可以获得较小的性能优势,因为这样保存SQLite不必制作输入字符串的副本。
ppStmt
[inout]ppStmt指向可以使用sqlite3_step执行的编译准备语句。如果有错误,ppStmt设置为NULL。如果输入文本不包含SQL(如果输入是空字符串或注释),则ppStmt设置为NULL。调用过程完成后,负责使用sqlite3_finalize删除编译的SQL语句。ppStmt可能不为空。
pszTail
[in]如果pzTail不为NULL,则指向szSql中第一个SQL语句结尾的第一个字节。这些函数只编译在szSql中的第一个语句,所以pzTail指向什么仍然未编译。
返回值
如果成功返回SQLITE_OK,否则返回错误代码。
C ++实现代码
#ifndef SQLITE_OMIT_UTF16
/*
** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
*/
static int sqlite3Prepare16(
sqlite3 *db, /* Database handle. */
const void *zSql, /* UTF-16 encoded SQL statement. */
int nBytes, /* Length of zSql in bytes. */
int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
const void **pzTail /* OUT: End of parsed string */
){
/* This function currently works by first transforming the UTF-16
** encoded string to UTF-8, then invoking sqlite3_prepare(). The
** tricky bit is figuring out the pointer to return in *pzTail.
*/
char *zSql8;
const char *zTail8 = 0;
int rc = SQLITE_OK;
assert( ppStmt );
*ppStmt = 0;
if( !sqlite3SafetyCheckOk(db) ){
return SQLITE_MISUSE_BKPT;
}
sqlite3_mutex_enter(db->mutex);
zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
if( zSql8 ){
rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
}
if( zTail8 && pzTail ){
/* If sqlite3_prepare returns a tail pointer, we calculate the
** equivalent pointer into the UTF-16 string by counting the unicode
** characters between zSql8 and zTail8, and then returning a pointer
** the same number of characters into the UTF-16 string.
*/
int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
*pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
}
sqlite3DbFree(db, zSql8);
rc = sqlite3ApiExit(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
/*
** Two versions of the official API. Legacy and new use. In the legacy
** version, the original SQL text is not saved in the prepared statement
** and so if a schema change occurs, SQLITE_SCHEMA is returned by
** sqlite3_step(). In the new version, the original SQL text is retained
** and the statement is automatically recompiled if an schema change
** occurs.
*/
SQLITE_API int sqlite3_prepare16(
sqlite3 *db, /* Database handle. */
const void *zSql, /* UTF-16 encoded SQL statement. */
int nBytes, /* Length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
const void **pzTail /* OUT: End of parsed string */
){
int rc;
rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
return rc;
}
SQLITE_API int sqlite3_prepare16_v2(
sqlite3 *db, /* Database handle. */
const void *zSql, /* UTF-16 encoded SQL statement. */
int nBytes, /* Length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
const void **pzTail /* OUT: End of parsed string */
){
int rc;
rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
return rc;
}
#endif /* SQLITE_OMIT_UTF16 */