SQLite Forum

how to save results of
Login

how to save results of

(1.1) Originally by hitbuyi with edits by Richard Hipp (drh) on 2020-05-23 10:51:51 from 1.0

static int callback_query1(void *NotUsed, int argc, char **argv, char **azColName)
{
   int i;
   for(i=0; i<argc; i++)
   {
      printf("%s = %s    ", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}


const char *sql_query_tmp ="SELECT FrmNo,CapTime,CamTime from radar_1 WHERE FrmNo = %d ";
   char sql_query[200] = {0};
   sprintf(sql_query,sql_query_tmp,1);
   printf("query,radar_1,sql = %s\n",sql_query);

   rc = sqlite3_exec(db, sql_query, callback_query1, 0, &zErrMsg);
   if (rc != SQLITE_OK)
   {
      printf("sql inquery error, %s", sqlite3_errmsg(db));

      sqlite3_free(zErrMsg);
   }
    


the code inquire the table well, and the call back function print the correct data.

BUT,how do I save this results into array or file? since callback function have no interface in sqlite3_exec(),should I write in this way

rc = sqlite3_exec(db, sql_query, callback_query1(p1,p2,...,etc), 0, &zErrMsg);

I feel it is a bit hard for a newbie to learn sqlite3, since few examples are available from the office website,in fact,even a few examples are helpful for a starter, 

Any help will be appricated

(2) By anonymous on 2020-05-23 15:04:27 in reply to 1.1 [link]

> the call back function print the correct data.
>
> BUT,how do I save this results into array or file

There is an argument to your callback function that you called `void *NotUsed`. Note that [it is passed from the second-to-last argument to `sqlite3_exec`](https://sqlite.org/c3ref/exec.html). You can use it to pass your callback a `FILE*` to `fprintf` into or a pointer to append data to.

Take a look at the [implementation of the `sqlite3_get_table()` function](https://sqlite.org/src/file?name=src/table.c&ci=tip) (not recommended for use) for some inspiration on how to allocate a memory for an unknown number of rows while receiving them in the callback. Perhaps the usual sequence of `sqlite3_prepare_v2()` / `sqlite3_bind_*()` / loop on `sqlite3_step()` / `sqlite3_finalize()` will result in cleaner code?

(3) By Clemens Ladisch (cladisch) on 2020-05-23 15:04:41 in reply to 1.1 [link]

The easiest way to use `sqlite3_exec()` is to replace it with `sqlite3_prepare_v2()`/`sqlite3_step()`/`sqlite3_column_*()`/`sqlite3_finalize()` calls so that you can read the data in the same place where you actually need to handle it:
```c
sqlite3_stmt *stmt;
const char *sql = "SELECT FrmNo,CapTime,CamTime from radar_1 WHERE FrmNo = ?";
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
    print("error: ", sqlite3_errmsg(db));
    return;
}
sqlite3_bind_int(stmt, 1, 1);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    int frm_no           = sqlite3_column_int (stmt, 0);
    const char *cap_time = sqlite3_column_text(stmt, 1);
    // ...
}
if (rc != SQLITE_DONE) {
    print("error: ", sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
```