andy
andy

Reputation: 195

Cannot copy sqlite file into bundle/app, resource for iPhone

I've been writing this app for iPhone and I need to use sqlite. I have pre-loaded my sqlite with a bunch of data, I copied that sqlite file into my project folder. And when I launch the app, it seems like the sqlite data file was not copied to the simulator's app file (the document place). Here's my code:

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"moviedbl.data"];
    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if(!success){
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"moviedbl.data"];
        //NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"moviedbl" ofType: @"data"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if(!success){
            NSAssert1(0, @"Failed to create writable database file with message '%'.", [error localizedDescription]);
        }
    }

It seems that defaultDBPath refers to the app folder in the simulator, which originally does not contain any sqlite file, and instead, is actually waiting for a sqlite file to be copied to it. From what I understand, to copy the file, we should get the file from our bundle (not sure if bundle refers to our project folder or what, but I assumed it is our project folder) and copy it to our simulator. I've been stuck on this for days, please enlighten me... Thankyou very much in advance!!!

UPDATE: So I actually got it working after all. At first I thought it was because I didn't copy the files from mainBundle or something, but it turns out that I did when I opened the actual folder of the .app in the path. I saw that the database it actually included. Something weird is that it doesn't have a file type, but in xCode it shows the file type as "data", so I kept using ofType: @"data". So in the end I changed it to ofType: @"" and it worked! Anyways! stupid me, but thanks for everyone who tried to help!! :D

Upvotes: 0

Views: 1887

Answers (1)

Krunal
Krunal

Reputation: 6490

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"moviedbl.data"];

if([fileManager fileExistsAtPath:dbPath]==NO){

    NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"moviedbl" ofType: @"data"];
   BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if(!success){
        NSAssert1(0, @"Failed to create writable database file with message '%'.", [error localizedDescription]);
    }
}

Upvotes: 1

Related Questions