Dear fellow apple iphone and Objective-C designers:

I'm attempting to build my abilities being an apple iphone developer, and presently I'm focusing on while using SQLite database. I've produced a SQLite table the following during my GroceryListAppDelegate.m class:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.database = [[[ISDatabase alloc] initWithFileName:@"TestDB.sqlite"] autorelease];

if(![[database tableNames] containsObject:@"GroceryItem"]) {

    [database executeSql:@"create table GroceryItem(primaryKey integer primary key autoincrement, name text NOT NULL, number integer NOT NULL)"];
    [database executeSql:@"insert into GroceryItem (name, number) values ('apples', 5)"];
    [database executeSql:@"insert into GroceryItem (name, number) values ('oranges', 3)"];


[window addSubview:navigationController.view];
[window makeKeyAndVisible];

I create a sql get in touch with my RootViewController.m class:

- (void)viewDidLoad {
[super viewDidLoad];

GroceryList1AppDelegate *appDelegate = (GroceryList1AppDelegate *)[[UIApplication sharedApplication] delegate];

self.results = [appDelegate.database executeSqlWithParameters:@"SELECT * from GroceryItem where number < ?", [NSNumber numberWithInt:6], nil];

My executeSqlWithParameters() method appears like this:

- (NSArray *) executeSql:(NSString *)sql withParameters: (NSArray *) parameters {

NSMutableDictionary *queryInfo = [NSMutableDictionary dictionary];
[queryInfo setObject:sql forKey:@"sql"];

if (parameters == nil) {

    parameters = [NSArray array];


//we now add the parameters to queryInfo

[queryInfo setObject:parameters forKey:@"parameters"];

NSMutableArray *rows = [NSMutableArray array];

//log the parameters

if (logging) {

    NSLog(@"SQL: %@ \n parameters: %@", sql, parameters);


sqlite3_stmt *statement = nil;

if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {

    [self bindArguments: parameters toStatement: statement queryInfo: queryInfo];

    BOOL needsToFetchColumnTypesAndNames = YES;
    NSArray *columnTypes = nil;
    NSArray *columnNames = nil;

    while (sqlite3_step(statement) == SQLITE_ROW) {

        if (needsToFetchColumnTypesAndNames) {

            columnTypes = [self columnTypesForStatement:statement];
            columnNames = [self columnNamesForStatement:statement];
            needsToFetchColumnTypesAndNames = NO;


        id row = [[NSMutableDictionary alloc] init];
        [self copyValuesFromStatement:statement toRow:row queryInfo:queryInfo columnTypes:columnTypes columnNames:columnNames];
        [rows addObject:row];
        [row release];



else {

    [self raiseSqliteException:[[NSString stringWithFormat:@"failed to execute statement: '%@', parameters: '%@' with message: ", sql, parameters] stringByAppendingString:@"%S"]];


return rows;

After I build and run my code, I recieve no results, much more fact, given my SQL call, I ought to get apples, and oranges during my list. However, after i modify my sql code for this:

self.results = [appDelegate.database executeSql:@"SELECT * from GroceryItem"];

Which calls the various method: executeSql():

- (NSArray *) executeSql: (NSString *)sql {

return [self executeSql:sql withParameters: nil];

Within this situation, I finish up obtaining the results: apples, and oranges. Why? What shall we be held doing wrong? Why do I get is a result of one sql call, and never from another?

Any help could be greatly appreciated.

Should you extract the .sqlite file in the Simulator or tool and open it up in around the command line using sqlite3, does the query work? This will allow you to split up testing your database creation code and query code.

Thanks greatly for all your help, however i found the answer. The issue is at my bindArguments() function:

- (void) bindArguments: (NSArray *) arguments toStatement: (sqlite3_stmt *) statement queryInfo: (NSDictionary *) queryInfo {

exactly where I'd the bind statement for amounts:

else if ([argument isKindOfClass:[NSNumber class]])
            sqlite3_bind_double(statement, -1, [argument doubleValue]);

I desired to alter the -1 for an i. This variables informs sql which variable I will susbstitute within the statement. So within this situation we simply have one variable symbolized by ? and it ought to be replaced with a 5.

Many thanks for all your help.