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



我正在尝试将宽字符串绑定到sqlite3准备好的语句。我试图遵循这个答案,但它不起作用

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: %sn", 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,但我得到的要么是null,要么是没有替换的原始字符串。我做错了什么?

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

更重要的是,根据文档,sqlite3_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: %sn", 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) ...

最新更新