iphone - Unable to retrieve and view results from a SQLite database -
dear fellow iphone , objective-c developers:
i trying build skills iphone developer, , presently working on using sqlite database. have created sqlite table follows in 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 groceryitem (name, number) values ('apples', 5)"]; [database executesql:@"insert groceryitem (name, number) values ('oranges', 3)"]; } [window addsubview:navigationcontroller.view]; [window makekeyandvisible];
}
i make sql call in rootviewcontroller.m class:
- (void)viewdidload { [super viewdidload]; grocerylist1appdelegate *appdelegate = (grocerylist1appdelegate *)[[uiapplication sharedapplication] delegate]; self.results = [appdelegate.database executesqlwithparameters:@"select * groceryitem number < ?", [nsnumber numberwithint:6], nil];
}
my executesqlwithparameters() method looks this:
- (nsarray *) executesql:(nsstring *)sql withparameters: (nsarray *) parameters { nsmutabledictionary *queryinfo = [nsmutabledictionary dictionary]; [queryinfo setobject:sql forkey:@"sql"]; if (parameters == nil) { parameters = [nsarray array]; } //we add parameters queryinfo [queryinfo setobject:parameters forkey:@"parameters"]; nsmutablearray *rows = [nsmutablearray array]; //log 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 { sqlite3_finalize(statement); [self raisesqliteexception:[[nsstring stringwithformat:@"failed execute statement: '%@', parameters: '%@' message: ", sql, parameters] stringbyappendingstring:@"%s"]]; } sqlite3_finalize(statement); return rows;
}
when build , run code, no results, when in fact, given sql call, should apples, , oranges in list. however, when modify sql code this:
self.results = [appdelegate.database executesql:@"select * groceryitem"];
which calls different method: executesql():
- (nsarray *) executesql: (nsstring *)sql { return [self executesql:sql withparameters: nil];
}
in case, end getting results: apples, , oranges. why this? doing wrong? why getting results 1 sql call, , not another?
any appreciated.
if extract .sqlite file simulator or device , open in on command line using sqlite3
, query work? enable separate testing database creation code , query code.
Comments
Post a Comment