i am beginner in objective c in my project i am using SQLite and here i am creating table programmatically using FMDB but when ever run the project it's shows exception like
"[NSFileManager copyItemAtPath:toPath:error:]: source path is nil"
please help me some one
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);
}
}
Now the path is not nil,If you try the below answer
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");
}
}
}
The Output
Test backup path is - /Users/name/Library/Developer/CoreSimulator/Devices/35CDFFA0-36A1-499D-8D17-7EF356F1151E/data/Containers/Bundle/Application/DEA0343A-05C8-4411-BF92-4000918E492A/DataBaseDemo.app/studentdb.sqlite
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.