confused why the sqlite step does not execute

Can anyone help, I am trying to execute a statement in the function completeChallenge however for some reason the statement never executes. No error happens at all and I am confused. If I put bogas text in the sql statement, no error occurs either so it seems as if it just does not even try?

completeChallenge is called by: checkCurrentChallengeCallback()


if I put completeChallenge in the main() it works. I am not sure what I am doing wrong as I am new to C++. Any help anyone?


int completeChallenge(int id) {


sqlite3 *db;
string breakpoint;
char *zErrMsg = 0;
int rc;
const char *sql;
const char* data = "";
sqlite3_stmt *stmt = NULL;
string completed = "n";



/* Open database */
rc = sqlite3_open("challenge.db", &db);


if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}
else {
fprintf(stderr, "Opened database successfully\n");
}

/* Create SQL statement */
sql = "UPDATE tblChallenge set isCurrent=? WHERE id=?";

//sqlite using variables in statement.
sqlite3_prepare_v2(db, sql, strlen(sql) + 1, &stmt, NULL);
//my god this took me ages to figure it out!
sqlite3_bind_text(stmt, 1, completed.c_str(), strlen(completed.c_str()), 0);

sqlite3_bind_int(stmt, 2, id);
//Execute parameter statement.

sqlite3_step(stmt);




//Clean up.
sqlite3_finalize(stmt);

sqlite3_close(db);


return 0;


}

int checkCurrentChallengeCallback() {

sqlite3 *db;
string breakpoint;
string challenge;
char *zErrMsg = 0;
int rc;
int id;
const char *sql;
const char* data = "";
sqlite3_stmt *stmt = NULL;

string myanswer;

/* Open database */
rc = sqlite3_open("challenge.db", &db);

if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}
else {
//fprintf(stderr, "Opened database successfully\n");
}

/* Create SQL statement */
sql = "SELECT id,challenge FROM tblChallenge WHERE isCurrent='y' LIMIT 1";
//sql = "SELECT * FROM tblChallenge ORDER BY RANDOM() LIMIT 1";
//sqlite using variables in statement.
sqlite3_prepare_v2(db, sql, strlen(sql) + 1, &stmt, NULL);
//sqlite3_bind_text(stmt, 1, answer.c_str(), strlen(answer.c_str()), 0);

system("CLS");

while (sqlite3_step(stmt) != SQLITE_DONE) {
int i;

int num_cols = sqlite3_column_count(stmt);



//i=1 means it won't display the id column which is 0.
for (i = 0; i < 2; i++)
{
switch (sqlite3_column_type(stmt, i))
{
case (SQLITE3_TEXT):
// printf(" ** %s ** ", sqlite3_column_text(stmt, i));
printf("\TYou already have a challenge which is: %s", sqlite3_column_text(stmt, i));
break;
case (SQLITE_INTEGER):

id = sqlite3_column_int(stmt, i);

break;
case (SQLITE_FLOAT):
printf(" %g, ", sqlite3_column_double(stmt, i));
break;
default:
break;
}
}
;

}
//Clean up.
sqlite3_finalize(stmt);

sqlite3_close(db);


cout << "\n\n Would you like to complete this challenge? (y/n): ";
cin >> myanswer;
if (myanswer == "y" || myanswer == "Y") {
cout << id << endl;
completeChallenge(id);

}


return 0;

}
I figured it out. I hadn't closed the existing connection before trying to run this function.

sqlite3_close(db);

PS. Love the retro style of this forum, breath of fresh air.
Topic archived. No new replies allowed.