与预先填充的sqlite的核心数据关系

时间:2013-03-15 03:50:28

标签: ios sqlite core-data

首先,抱歉。我英语说的不好。你好。我是iOS的初学者。

目前我的开发方式如下:

  1. 删除自动生成的核心数据sqlite文件
  2. 使用firefox sqlite manager将我创建的sqlite文件复制到已删除的sqlite文件路径(使用Z_PK,Z_ENT,Z_OPT进行sqlite)
  3. 两个问题:

    1. 如何使用firefox sqlite manager创建实体关系。
    2. 如果有详细的核心数据选项,如关系删除规则,sqlite管理器上的属性属性或其他任何内容,请如何预创建。

2 个答案:

答案 0 :(得分:0)

好吧,基本上你需要掌握一个由你的项目使用你的模式创建的空数据库,然后操纵它来保存你想要的数据。更多信息here

链接中未描述的替代方法是使用Core Data本身(以及用于读取其他数据库的SQLite库)将数据从数据库复制到Core Data数据库中。

答案 1 :(得分:0)

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

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataBooks.CDBStore"];

/*
 Set up the store.
 For the sake of illustration, provide a pre-populated default store.
 */
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:[storeURL path]]) {
    NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"CoreDataBooks" withExtension:@"CDBStore"];
    if (defaultStoreURL) {
        [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
    }
}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

NSError *error;
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.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator;

代码取自Apple docs Core Data Books Link 您需要预先填充已通过Core Data填充的数据库。该应用程序首次启动时,此功能会将预先填充的数据库复制到主DB存储位置。