When I type into Sqlite
SELECT Count(*) FROM tabl;
it returns a number.
How do I use the C interface to obtain this number after preparing a statement?
Open the database file, prepare the statement, make steps (i.e., if you have more than one row in results you have to fetch them one by one), extract column values, finalize the statement, close the database.
Something like this:
sqlite3_stmt* stmt = NULL;
sqlite3* local_db = NULL;
sqlite3_open("filename.sqlite", &local_db);
int retval, idx;
char sql[2048];
sprintf(sql, "select Something from Somewhere;");
// execute statement
retval = sqlite3_prepare_v2(local_db, sql, -1, &stmt, 0);
if(retval)
{
printf("Selecting data from DB Failed (err_code=%d)\n", retval);
return;
}
// iterate rows
idx = 0;
// for multiple results
while(1)
{
// fetch a row's status
retval = sqlite3_step(stmt);
if(retval == SQLITE_ROW)
{
Something =
(int)sqlite3_column_int(stmt, 0);
// or other type - sqlite3_column_text etc.
// ... fetch other columns, if there are any
}
else if(retval == SQLITE_DONE)
{
break;
}
else
{
sqlite3_finalize(stmt);
printf("Some error encountered\n");
break;
}
}
sqlite3_finalize(stmt);
sqlite3_close(local_db);
Use this code, look for all the API calls (open, prepare_v2, step, column, finalize).
If this is hard, then at first you should become familiar with C itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With