我是项目中目标c的初学者我正在使用SQLite,在这里我使用FMDB以编程方式创建表格,但是当运行项目时,它显示异常,如
" [NSFileManager copyItemAtPath:toPath:error:]:源路径为nil"
请帮帮我一个
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *dbUrl = [fileManager URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask appropriateForURL:nil
create:NO error:nil];
dbUrl = [dbUrl URLByAppendingPathComponent:@"studentdb.sqlite"];
FMDatabase * db = [FMDatabase databaseWithPath:dbUrl.absoluteString];
if([db open]){
NSString *dbCarTableCreate = @"CREATE TABLE IF NOT EXISTS studentInfo1(rollnum integer primary key autoincrement,name text,age text)";
if([db executeUpdate:dbCarTableCreate]){
NSLog(@"Table create success");
}
else{
NSLog(@"%@",db.lastError);
NSLog(@"%@",db.lastErrorMessage);
}
}
答案 0 :(得分:0)
现在路径不是零,如果您尝试以下答案
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *documentsDirectory = [documentsDir stringByAppendingPathComponent:@"studentdb.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
{
NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"studentdb" ofType:@"sqlite"];
NSLog(@"Test backup path is - %@",backupDbPath);
if (backupDbPath == nil)
{
NSLog(@"Database path is nil");
}
else
{
BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:documentsDirectory error:nil];
if (!copiedBackupDb){
NSLog(@"Copying database failed");
}
}
}
输出
测试备份路径是 - /Users/name/Library/Developer/CoreSimulator/Devices/35CDFFA0-36A1-499D-8D17-7EF356F1151E/data/Containers/Bundle/Application/DEA0343A-05C8-4411-BF92-4000918E492A/DataBaseDemo.app/studentdb.sqlite