2

我有一个带有预加载数据的 Sqlite 文件。我正在使用 Core Data 从 Sqlite 文件中读取和显示数据。

这是我读取数据的方式:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL;


        NSString *path = [[NSBundle mainBundle] pathForResource:@"myAppname" ofType:@"sqlite"];
        storeURL = [NSURL fileURLWithPath:path];



    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    // This dictionary automatically updates and migrates the store if it changes
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             [NSNumber numberWithBool: NO], NSReadOnlyPersistentStoreOption,
                             nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        if (error) {
            [myErrorAlert showError:error];
        }

        CLS_LOG(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

但是,我需要使用我在应用程序之外使用 sqlite 数据编辑器执行的新数据来更新 Sqlite 文件。

现在,当我复制粘贴并覆盖“myAppname.sqlite”文件时,出现以下错误:

CoreData: error: (14) I/O error for database at /Users/Username/Library/Application Support/iPhone Simulator/7.1/Applications/45C373D7-8A54-48EF-BA3C-2E2C6AB7467F/myAppname.app/myAppname.sqlite.  SQLite error code:14, 'unable to open database file'

我确实尝试添加NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"},但仍然存在相同的错误。

还有一些答案建议删除对文件的引用并创建一个新文件,但我不知道该怎么做。

任何想法?

编辑

我设法让它工作。

在 iOS 6 模拟器上打开原始应用程序。

用新文件替换 Sqlite 文件并仅运行 App iOS 6 模拟器,而不是在 iOS7 模拟器上运行。没有任何问题。

但是仍然想知道其他评论。

4

0 回答 0