I am creating database in my app,after creating the Db,i am trying to copy the db from the app bundle to Doc directory.But i could not copy it,i am getting error like The operation couldn’t be completed. (Cocoa error 260.) Below shown is the code what i am using,please help me to fix this issue
dbName="userDetails.db";
returnCode=sqlite3_open(dbName,&handler);
if(returnCode!=SQLITE_OK)
NSLog(@"message---%s",sqlite3_errmsg(handler));
else
NSLog(@"sucess---%d",returnCode);
NSFileManager *fmgr=[NSFileManager defaultManager];
NSError *error;
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *docPath=[path stringByAppendingPathComponent:@"userDetails.db"];
if(![fmgr fileExistsAtPath:docPath]){
NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"userDetails.db"];
if(![fmgr copyItemAtPath:defaultDBPath toPath:docPath error:&error])
NSLog(@"failure message----%@",[error localizedDescription]);
}
5 Answers 5
Actually I am little confuse with your code. Are you trying to programmatically create a database in the app bundle and then copying it to documents directory? If this is what you are doing then you need to know that you can't create or modify anything in app bundle. You must create this database in Documents directory. You should do something like this -
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString * databasePath = [documentsDir stringByAppendingPathComponent:@"XYZ.sqlite"];
// generate databasePath programmatically
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// your code here
}
But if you already have a database in your app (and not dynamically creating) and you just want to copy it to documents directory then you should follow @Santosh Gurram answer.
Comments
Try the below code,
- (void) copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"xyz.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"XYZ.sqlite"];
}
Add the -(void)CopyDatabaseIfNeeded in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions appDelegate method.
Comments
Two things come to mind:
- Trying logging the path names (docPath and defaultDBPath). Are they correct?
You have opened the db file, maybe it is not letting you copy due to this. Try commenting the following lines:
returnCode=sqlite3_open(dbName,&handler);
if(returnCode!=SQLITE_OK)
NSLog(@"message---%s",sqlite3_errmsg(handler));else
NSLog(@"sucess---%d",returnCode);
Comments
Consider using Core Data and then use a JSON serialized file for your initial payload?
Comments
Hi I had the same problem because when I added the db.sqlite to the project bundle I forgot check the add to the project option. Check this :)