1

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]);
}
asked Nov 25, 2011 at 6:49

5 Answers 5

1

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.

answered Nov 25, 2011 at 11:43
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Saurabh
22.9k12 gold badges87 silver badges133 bronze badges
answered Nov 25, 2011 at 11:27

Comments

0

Two things come to mind:

  1. Trying logging the path names (docPath and defaultDBPath). Are they correct?
  2. 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);
    
answered Nov 25, 2011 at 7:22

Comments

0

Consider using Core Data and then use a JSON serialized file for your initial payload?

answered Nov 25, 2011 at 9:08

Comments

0

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 :)

answered Oct 29, 2012 at 23:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.