Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:18:13+00:00 2026-06-11T04:18:13+00:00

I want to copy the database file from bundle to user document. My code

  • 0

I want to copy the database file from bundle to user document.

My code is below:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *userPath = [documentsDir stringByAppendingPathComponent:@"db.sql"];

NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Bundle database exists: %i",[fileManager fileExistsAtPath:srcPath]);
NSLog(@"User Document folder database exists: %i",[fileManager fileExistsAtPath:userPath]);


BOOL find = [fileManager fileExistsAtPath:userPath];
BOOL copySuccess = FALSE;

NSError *error;

if (!find) {
    NSLog(@"don't have writable copy, need to create one");
    copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
}

if (!copySuccess) {

    NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
}

and the result is always saying:

Bundle database exists: 1 User Document folder database exists: 0

don’t have writable copy, need to create one Failed with message: ‘The
operation couldn’t be completed. (Cocoa error 4.)’.

Please suggest, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:18:14+00:00Added an answer on June 11, 2026 at 4:18 am

    Your code for determining your user’s Documents Directory is incorrect.

    Using your code, I put together a quick and dirty sample that works. For your application, you probably want to create some utils class that contains the static function ‘applicationDocumentsDirectory’ so that other classes in your project can call it, if needed.

    Header File:

    #import <UIKit/UIKit.h>
    
    @interface TST_ViewController : UIViewController
    
    + (NSString *) applicationDocumentsDirectory;
    
    @end
    

    Implementation File:

    #import "TST_ViewController.h"
    
    @interface TST_ViewController ()
    
    @end
    
    @implementation TST_ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSString *dbFilename = @"db.sql";
        NSString *userPath = [[TST_ViewController applicationDocumentsDirectory] stringByAppendingPathComponent:dbFilename];
        NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFilename];
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        BOOL foundInBundle = [fileManager fileExistsAtPath:srcPath];
        BOOL foundInUserPath = [fileManager fileExistsAtPath:userPath];
        BOOL copySuccess = FALSE;
    
        NSError *error;
    
        if(foundInBundle) {
    
            if (!foundInUserPath) {
                NSLog(@"Don't have a writable copy, so need to create one...");
                copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
            }
    
            if (!copySuccess) {
                NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
            }
    
        } else {
            // handle error in the event the file is not included in the bundle
        }
    
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    + (NSString *) applicationDocumentsDirectory
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        return basePath;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I want to copy a selection of lines from a file to another file
I want to copy a xml file from one remote box to a bunch
I want to detach the database from SQL Server 2005 from my C# code.
How to copy from one database to another database. Database name visco I want
I want to copy an excel file to my database. The problem I'm having
I use ZODB and i want to copy my 'database_1.fs' file to another 'database_2.fs'
I have a database schema in SQL 2005 that I want to copy to
I want to copy few files into an existing zip file using ANT script.
I want to copy directories and their subdirectories from C: to D: with same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.