我用下面的代码从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