I'm trying to pass a string to a function that uses sqlite3_bind_text. When I check the database, the first char of the string is replaced by a unicode NULL char (if I interpret the rectangle symbol containing four zeroes in each corner correctly).
Here is a minimal example. The cout in the function outputs the string correctly, as does using sqlite3_bind_text directly in main.
#include "sqlite3.h"
#include <iostream>
using namespace std;
void bind_string_to_statement(sqlite3_stmt *& statement, const string s)
{
// This replaces the first char in s with [0000]...
sqlite3_bind_text(statement, 1, s.c_str(), -1, nullptr);
// ...although this looks fine:
cout << s.c_str() << endl;
}
int main()
{
sqlite3 * db = nullptr;
sqlite3_open("test.sqlite", &db);
sqlite3_stmt * statement = nullptr;
string query
{ "create table if not exists t(key text primary key not null, value text)" };
sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);
sqlite3_step(statement);
sqlite3_finalize(statement);
query = "insert or replace into t(key, value) values(?, ?)";
sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);
string s{ "stuff" };
// This fails as described in the function above...
bind_string_to_statement(statement, s);
// ...although this works fine:
// sqlite3_bind_text(statement, 1, s.c_str(), -1, nullptr);
sqlite3_step(statement);
sqlite3_finalize(statement);
sqlite3_close(db);
return 0;
}
What could be the problem when using sqlite3_bind_text in a function?
Aucun commentaire:
Enregistrer un commentaire