Iphone 在设备和应用商店上刷新SQLITE3(核心数据)

Iphone 在设备和应用商店上刷新SQLITE3(核心数据),iphone,objective-c,core-data,sqlite,refresh,Iphone,Objective C,Core Data,Sqlite,Refresh,我有一个利用核心数据SQLITE3的应用程序,它在模拟器中运行得非常好。但是,我不知道如何更新设备上的DB,我猜这与app store中的相同 我从应用程序中的.txt文件更新DB并创建DB,该函数仅用于创建DB,并将在最终版本中删除。我的想法是在模拟器中创建DB,锁定代码的更新部分,然后用更新的数据库分发包 但是,当我在设备上重建我的应用程序时,数据库中仍然有旧数据 我一直在四处寻找,但恐怕我不完全明白如何解决这个问题。我确实找到了这个线索: 如果有人能告诉我一些事情并帮助我解决这个问题,我将

我有一个利用核心数据SQLITE3的应用程序,它在模拟器中运行得非常好。但是,我不知道如何更新设备上的DB,我猜这与app store中的相同

我从应用程序中的.txt文件更新DB并创建DB,该函数仅用于创建DB,并将在最终版本中删除。我的想法是在模拟器中创建DB,锁定代码的更新部分,然后用更新的数据库分发包

但是,当我在设备上重建我的应用程序时,数据库中仍然有旧数据

我一直在四处寻找,但恐怕我不完全明白如何解决这个问题。我确实找到了这个线索:

如果有人能告诉我一些事情并帮助我解决这个问题,我将非常感激


干杯

您是否已将db文件从只读捆绑目录复制到可写目录?就像每个应用程序的文档目录一样

当您尝试在设备中保存时,是否出现类似这样的sqlite错误

SQLITE_READONLY     8   /* Attempt to write a readonly database */
编辑:

主捆绑包中的所有文件都是只读的,因此如果需要修改其中的一个/部分文件,则需要将文件复制到可写的位置。假设您调用了db mydb.sqlite,这里有一些代码仅在数据库不存在时将其复制到documents目录

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
        }
    }

您是否已将db文件从只读捆绑包目录复制到可写目录?就像每个应用程序的文档目录一样

当您尝试在设备中保存时,是否出现类似这样的sqlite错误

SQLITE_READONLY     8   /* Attempt to write a readonly database */
编辑:

主捆绑包中的所有文件都是只读的,因此如果需要修改其中的一个/部分文件,则需要将文件复制到可写的位置。假设您调用了db mydb.sqlite,这里有一些代码仅在数据库不存在时将其复制到documents目录

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
        }
    }

实际上,在包中使用.db文件是一个非常糟糕的主意。 每次,当我使用.db文件时,我都会检查它是否存在于我的应用程序文档目录中,然后我会重写它

    #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;

实际上,在包中使用.db文件是一个非常糟糕的主意。 每次,当我使用.db文件时,我都会检查它是否存在于我的应用程序文档目录中,然后我会重写它

    #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;

谢谢朱利亚诺的回答,不,我没有,恐怕我真的不明白怎么做?谢谢朱利亚诺的回答,不,我没有,恐怕我真的不明白怎么做?