c ++将宽字符串绑定到sqlite3准备好的语句

c++ Bind wide string to sqlite3 prepared statement

我正在尝试将宽字符串绑定到 sqlite3 准备语句。我试图遵循 this 的答案,但没有成功

    const auto sql_command = L"SELECT * FROM register_names WHERE name is ?VVV";
    sqlite3_stmt *statement;
    sqlite3_prepare16(db, sql_command, -1, &statement, NULL);
    wstring p = ObjectAttributes->ObjectName->Buffer;
    sqlite3_bind_text16(statement, 1,  p.data(), -1, SQLITE_TRANSIENT);
    printf("sql command: %s\n", sqlite3_sql(statement));
    auto data = "Callback function called";
    char *zErrMsg = nullptr;

    auto rc = sqlite3_exec(db,  sqlite3_sql(statement), callback, (void *) data, &zErrMsg);

我尝试在 sqlite3_bind_text16 中使用 0 或 1,但我要么得到空字符串,要么得到没有替换的原始字符串。我做错了什么?

在您的 SQL 语句中,将 is 更改为 =,并将 ?VVV 更改为 ?

更重要的是,根据 documentationsqlite3_exec() 不是执行您准备的 sqlite3_stmt 的正确方法。您需要改用 sqlite3_step()(和 sqlite3_finalize())。

试试这个:

const auto sql_command = u"SELECT * FROM register_names WHERE name = ?";

sqlite3_stmt *statement;
auto rc = sqlite3_prepare16(db, sql_command, -1, &statement, NULL);
if (rc != SQLITE_OK) ...

rc = sqlite3_bind_text16(statement, 1, ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length, SQLITE_TRANSIENT);
if (rc != SQLITE_OK) ...

printf("sql command: %s\n", sqlite3_sql(statement));

while ((rc = sqlite3_step(statement)) == SQLITE_ROW)
{
    // process row as needed using sqlite3_column_XXX() functions...
}

if (rc != SQLITE_DONE) ...

rc = sqlite3_finalize(statement);
if (rc != SQLITE_OK) ...