I have table USER with columns ID, NAME and ADDRESS. e.g.
Column ID has 1,2 Column NAME has Bob, Jane Column ADDRESS has UK, US
And I wish to iterate through all records and save each column into an array. The result would be something like this:
char *NAME[2]; NAME[0] would have "Bob", NAME[1] would have "Jane" and
char *ADDRESS[2]; ADDRESS[0] would have "UK", ADDRESS[1] would have "US".
Can someone point me in the right direction instead of calling an SQL statement for each array entry?
Thanks in advance!
If you start with the SELECT example in the tutorial you linked to, you can modify it to have a
struct results {
size_t num_used;
char* NAME[SOME_MAX_NUMBER_OF_ROWS_HERE];
char* ADDRESS[SOME_MAX_NUMBER_OF_ROWS_HERE];
};
and then have one of those on your stack (with num_used set to 0) and pass a pointer to that as argument 4 of sqlite3_exec
. In callback
, you then cast argument 1 to a pointer to struct results
, and strdup
your fields to results->NAME[results->num_used]
etc., and increment results->num_used
at the end. When sqlite3_exec
returns, you should have your NAME
and ADDRESS
arrays filled out.
There are of course better ways of handling the arrays, so that you don't have to anticipate the maximum number of rows etc., but that's a different issue.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments