我试图创建第一个应用程序的启动数据库文件。使用FMDB库。为此,我正在检查数据库文件是否已经存在,如果没有,我想从项目中复制数据库文件。 我的代码:创建数据库文件,如果不存在
-(void)checkAndCreateDatabase {
NSLog(@"check and create database doing");
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbFileName = [docDir stringByAppendingPathComponent:@"FriendsDatabase.sqlite3"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if (![fileManager fileExistsAtPath:dbFileName]) {
[fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"FriendsDatabase" ofType:@"sqlite3"] toPath:dbFileName error:&error];
NSLog(@"database created");
} else {
NSLog(@"fail to create database");
}
FMDatabase *_db = [[FMDatabase alloc] initWithPath: dbFileName];
if (![_db open]) {
[_db release];
NSLog(@"Could't open DB");
}
_db.logsErrors = NO;
self.db = _db;
[_db release];
}
但我有一个错误:'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'
。我做错了什么?
这是不可能的。我在我的项目中添加了“FriendDatabase.sqlite3”。我检查了/ myProject /目录中的数据库文件的位置。 – RomanHouse
看到我上面的修改。 – Caleb