开发者

refresh SQLITE3 (Core Data) on device and app store

I have an app leveraging Core Da开发者_运维问答ta SQLITE3 that works perfectly in the simulator. However i do not understand how to update the DB on the device, which i guess is the same as in app-store.

I update the DB from .txt files in the app and create the DB, this function is there only for creating the DB and will be removed in the final version. My idea is to create the DB in the simulator, lock the update part of the code and then distribute the package with an updated database.

However, when i rebuild my app on the device it still have the old data in the DB.

I have been looking around but i am afraid i do not fully understand how to solve this. I did find this thread: Can't refresh iphone sqlite3 database

I would very much appreciate if some nice person could share some light on this and help me to solve this.

Cheers


Have you copied the db file from the bundle directory (which is read only) to a writable one? (like the documents directory of each application?).

When trying to save in the device did you get a sqlite error like this?

SQLITE_READONLY     8   /* Attempt to write a readonly database */

EDIT:

All the files in the main bundle are read only, so if you need to modify one/some of them, you need to copy the files in a location that is writable. Assuming you have called the db mydb.sqlite here is some code that copies the db (only if it does not exists) to the documents directory.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *docPath = [docDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
    if (![fm fileExistsAtPath:docPath]) { // file does not exists, copy it
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
        NSError *error = nil;
        BOOL res = [fm copyItemAtPath:bundlePath toPath:docPath error:&error];
        if (!res) {
            // do something with error
        }
    }


Actually to use .db file inside the Bundle - it's a very bad idea. Every thime, when I am using .db file, i am checking, if it allready exists inside my Application document directory, and then I will rewrite it.

    #define DB_SHOULD_BE_REWRITTEN YES //You should update database and change allready existing db file to file from bundle
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];


    BOOL success = [fileManager fileExistsAtPath:writableDBPath];

    if (!success || DB_SHOULD_BE_REWRITTEN)
    {
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }

    dbPath = writableDBPath;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜