dimanche 4 janvier 2015

Sqlite doesn't find row _only_ when binding takes places in another function

So I wrote myself a little wrapper function to make a prepared statement for me:



sqlite3_stmt* Gladiateur::run_query_unfinalized(string query, vector<string> inputs){

sqlite3_stmt *statement;

// Prepare SQL
sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);

// Bind parameter
sqlite3_bind_text(statement, 1, inputs[0].c_str(), -1, NULL);

return statement;

}


Normally it would go through the inputs vector and bind all of the strings, but I simplified the code to find out where the error lies.


I have another function set_list (also simplified):



int Gladiateur::set_list (string list_name) {

vector<string> bind = {list_name};
sqlite3_stmt *statement = this->run_query_unfinalized("SELECT id FROM lists WHERE name = ? LIMIT 1", bind);

printf("code %d\n", sqlite3_step(statement));

sqlite3_finalize(statement);

return 1;

}


I call set_list, and I get "code 101", which simply implies that there were no SQL-errors but no row was retrieved. I know that a matching row exists, though.


And here comes the weird part: I move the line


sqlite3_bind_text(statement, 1, inputs[0].c_str(), -1, NULL);


into the set_list function, just above the printf, and write bind[0] instead of inputs[0]. And it works! I get "code 100", the row is found, it also gave me the correct id when I checked.


But I want to do all the binding in run_query_unfinalized... and I am really confused why it doesn't work. Maybe I can't pass the statement variable like that?


Aucun commentaire:

Enregistrer un commentaire