In the previous section, we saw how the function sqlite3_exec()
can be used in executing SQL statements that either do not produce
results, or the caller is not interested in processing the results.
If you are interested in the result set, however, you can pass a callback function pointer as the fourth parameter to the sqlite3_exec() function. This callback function will be invoked for every row in the result set.
The callback function should follow the following signature:
int (*callback)(void*,int,char**,char**)
The first parameter of this function is the same as the fourth parameter when the sqlite3_exec()
function is invoked. The second parameter is the number of columns in
the current row result. The third parameter is an array of pointers to
strings holding the values for each column in the current result set
row. The fourth parameter is an array of pointers to strings holding
the names of result columns. If the callback function returns a value
other than zero, the sqlite3_exec() function will stop executing and will return SQLITE_ABORT.
In the function main() shown in Listing 1, we demonstrate how a callback function can be used to process the result set. The database financial.db is opened as we have seen before and a SELECT query is executed. The query
SELECT* from stocks
retrieves all the records in the table stocks. The SQLite function call for executing the statement is as follows:
returnCode = sqlite3_exec(pDb,sqlStatement,processRow, NULL,&errorMsg);
The third parameter is not NULL as we saw in the previous section. Instead, we pass in the function pointer processRow. The function processRow() is shown in Listing 2.
Example 1. The function main() for retrieving records using sqlite3_exec() function.
#import "/usr/include/sqlite3.h" int main(int argc, char * argv[]) { char *sqlStatement; sqlite3 *pDb; char *errorMsg; int returnCode; char *databaseName;
databaseName = "financial.db"; returnCode = sqlite3_open(databaseName, &pDb); if(returnCode!=SQLITE_OK) { fprintf(stderr, "Error in opening the database. Error: %s", sqlite3_errmsg(pDb)); sqlite3_close(pDb); return −1; } sqlStatement = "SELECT * from stocks"; returnCode = sqlite3_exec(pDb,sqlStatement,processRow,NULL,&errorMsg); if(returnCode!=SQLITE_OK) { fprintf(stderr, "Error in selecting from stocks table. Error: %s", errorMsg); sqlite3_free(errorMsg);
} sqlite3_close(pDb); return 0; }
|
This function follows the callback function
signature. Inside the function, we have a for-loop where we display the
column name, and the row value for that column.
The result of executing the program is:
Record Data:
The value for Column Name symbol is equal to ALU
The value for Column Name purchasePrice is equal to 14.23
The value for Column Name unitsPurchased is equal to 100
The value for Column Name purchase_date is equal to 03-17-2007
Record Data:
The value for Column Name symbol is equal to GOOG
The value for Column Name purchasePrice is equal to 600.77002
The value for Column Name unitsPurchased is equal to 20
The value for Column Name purchase_date is equal to 01-09-2007
Record Data:
The value for Column Name symbol is equal to NT
The value for Column Name purchasePrice is equal to 20.23
The value for Column Name unitsPurchased is equal to 140
The value for Column Name purchase_date is equal to 02-05-2007
Record Data:
The value for Column Name symbol is equal to MSFT
The value for Column Name purchasePrice is equal to 30.23
The value for Column Name unitsPurchased is equal to 5
The value for Column Name purchase_date is equal to 01-03-2007
Example 2. The function processRow() for processing row results.
#import "/usr/include/sqlite3.h"
static int processRow(void* argument, int argc, char* *argv, char* *colName){ printf("Record Data:\n"); for(int i=0; i<argc; i++){ printf("The value for Column Name %s is equal to %s\n", colName[i], argv[i] ? argv[i] : "NULL"); } printf("\n");
return 0; }
|