我正在尝试在应用程序首次加载时创建/打开一个 sqlite 数据库。但是由于未捕获的异常'NSInternalInconsistencyException',我经常收到错误终止应用程序,原因:'错误:无法打开,因为库例程调用顺序错误' 下面是我在主视图控制器文件中的代码:
- (NSString *) filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"bp.sql"];
}
- (void)openDB {
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert1(0, @"Error: Failed to open because %s", sqlite3_errmsg(db));
}
else {
NSLog(@"Database opened");
}
}
- (void)viewDidLoad {
[self openDB];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}