Michael Chen
Michael Chen

Reputation: 15

Error copying file from app bundle

I used the FireFox add-on SQLite Manager, created a database, which saved to my desktop as "DB.sqlite". I copied the file into my supporting files for the project. But when I run the app, immediately I get the error

"Assertion failure in -[AppDelegate copyDatabaseIfNeeded], /Users/Mac/Desktop/Note/Note/AppDelegate.m:32 2014-08-19 23:38:02.830 Note[28309:60b] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 4.)'.' First throw call stack: "...

Here is the App Delegate Code where the error takes place

-(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:@"DB.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

    }
}

I am very new to Sqlite, so I maybe I didn't create a database correctly in the FireFox Sqlite manager, or maybe I didn't "properly" copy the .sqlite file in? (I did check the target membership in the sqlite and it correctly has my project selected. Also, the .sqlite file names all match up perfectly.)

Upvotes: 0

Views: 420

Answers (1)

Suraj Mirajkar
Suraj Mirajkar

Reputation: 1391

In iOS, you can't write a file in your app's bundle. The entire bundle is read-only. You should create the database (sqlite file) inside the NSDocumentDirectory in order to work on it.

Please visit site below for tutorial: http://objectivecwithsuraj.blogspot.in/2012/10/database-implementation-using-sqlite-in.html

Sample Code: https://github.com/surajwebo/SQLite-Demonstration-

Upvotes: 1

Related Questions