【发布时间】:2010-11-14 15:17:44
【问题描述】:
我使用下面的代码从 file.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 - (void)applicationDidFinishLaunching:(UIApplication *)application{
[self copyDatabaseIfNeeded];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.coffeeArray = tempArray;
[tempArray release];
[Coffee getInitialDataToDisplay:[self getDBPath]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
【问题讨论】:
-
你确定 appDelegate.coffeeArray 不是 nil 吗?还有,sqlite3_open 报错了吗? sqlite3_prepare_v2 报错了吗?
-
请查看已编辑的问题
-
使用FMDB。
标签: iphone sql objective-c sqlite