Fat Noodle
Fat Noodle

Reputation: 105

Correct usage of sqllite3_bin_text

I'am writing a small application (Login Mask) to get comfortable with the usage of SQLite3. Right now I have a problem with the correct usage of sqlite3_bind_text(). I've create a small DB with only 1 row. In this part of the code I would like to bind the user input to a variable and bind it to a statement. The user input delivered is by the getline(cin,variable) function.

My problem: I get an instant "False Library use" when I try to use the bind method. The result is always 21. I have read the API documentation several times, but I obviously don't understand the last part.

Can someone please show me how to correctly use this function(s)? I've checked my column types and they are "Text NOT NULL".

int Login::check_login(string username, string password) {
int errcode = 0;
string query = "Select a_username,a_password from t_user where a_username = ? and a_password = ?;";
sqlite3_stmt *createStmt;

errcode = sqlite3_prepare_v2(datab->get_db(), query.c_str(), query.size(), &createStmt, NULL);

if (!errcode)
    throw runtime_error("Failed! Can not prepare statement DB.");

errcode = sqlite3_bind_text(createStmt, 1, password.c_str(), -1, SQLITE_STATIC); // Always 21 because of false library use

//there is more code but i think this should be enough

P.S. I've googled this 2 days and found no solution / easy explanation of my problem.

Upvotes: 0

Views: 54

Answers (1)

Ivan
Ivan

Reputation: 2057

I think your call to sqlite3_prepare_v2() fails and doesn't prepare valid statement (you don't get exception, don't you?), but there is a typo in error check.

When sqlite3_* function succeeds, it returns SQLITE_OK which is 0. So correct error check is:

if (errcode)
    throw runtime_error("Failed! Can not prepare statement DB.");

That is why sqlite3_bind_text() also fails.

Upvotes: 2

Related Questions