我用下面的代碼從file.sql中讀取數據沒有發生什麼問題?SQL讀取數據?
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select coffeeID, coffeeName from coffee";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.CoffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.isDirty = NO;
[appDelegate.coffeeArray addObject:coffeeObj];
[coffeeObj release];
}
}
}
else
sqlite3_close(database);
}
appDelegate.m - (空)的applicationDidFinishLaunching:(UIApplication的*)應用程序{
[self copyDatabaseIfNeeded];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.coffeeArray = tempArray;
[tempArray release];
[Coffee getInitialDataToDisplay:[self getDBPath]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
你確定appDelegate.coffeeArray不是零嗎?另外,是sqlite3_open報告錯誤? sqlite3_prepare_v2是否報告錯誤? – 2010-11-13 19:29:36
請參閱編輯的問題 – 2010-11-13 19:35:45
使用[FMDB](http://github.com/ccgus/fmdb)。 – 2010-11-13 20:32:30