programming4us
programming4us
MOBILE

iphone SDK 3 : Working with Databases - Processing Row Results

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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;
}


Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us