Skip to content

Instantly share code, notes, and snippets.

Created August 7, 2011 07:35
Show Gist options
  • Save anonymous/1130160 to your computer and use it in GitHub Desktop.
Save anonymous/1130160 to your computer and use it in GitHub Desktop.
Fetch data from sqlite
-(NSMutableArray *) getChapters{
sqlite3 *database = Pc2MacDatabase();
NSMutableArray *chapters = [[[NSMutableArray alloc] init] autorelease];
NSString *nsquery = [[NSString alloc] initWithFormat:@"SELECT * FROM CHAPTER ORDER BY ID"];
const char *query = [nsquery UTF8String];
[nsquery release];
sqlite3_stmt *statement;
int prepareCode = (sqlite3_prepare_v2( database, query, -1, &statement, NULL));
if(prepareCode == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
Chapter *newChapter= [[Chapter alloc] init];
newChapter.chapterNumber = sqlite3_column_int(statement, 0);
newChapter.chapterName = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
newChapter.isFree = sqlite3_column_int(statement, 2);
[chapters addObject:newChapter];
[newChapter release];
}
sqlite3_finalize(statement);
}
return chapters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment