Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将预先存在的sqlite文件导入核心数据?_Iphone_Ios_Sqlite_Core Data - Fatal编程技术网

Iphone 如何将预先存在的sqlite文件导入核心数据?

Iphone 如何将预先存在的sqlite文件导入核心数据?,iphone,ios,sqlite,core-data,Iphone,Ios,Sqlite,Core Data,我需要将.sqlite文件导入核心数据,我在互联网上搜索发现: 它通过读取旧数据库的内容并在新数据库中创建适当的行来创建一个Python脚本来填充这个数据库。但就表和列的数量而言,我的sqlite数据库太大,这可能会花费我大量的时间 我还发现: 但我不太明白,它看起来像是在将旧数据库复制到新数据库,那么它如何为所有表名和列名添加Z_uu后缀呢?另外,它要求我创建实体和属性,是否有任何方法可以自动完成(从sqlite dabase文件) 谢谢 这里的答案可能有用(我的答案就是其中之一) 不,

我需要将.sqlite文件导入核心数据,我在互联网上搜索发现:

它通过读取旧数据库的内容并在新数据库中创建适当的行来创建一个Python脚本来填充这个数据库。但就表和列的数量而言,我的sqlite数据库太大,这可能会花费我大量的时间

我还发现:

但我不太明白,它看起来像是在将旧数据库复制到新数据库,那么它如何为所有表名和列名添加Z_uu后缀呢?另外,它要求我创建实体和属性,是否有任何方法可以自动完成(从sqlite dabase文件)


谢谢

这里的答案可能有用(我的答案就是其中之一)


不,没有任何自动方式。在核心数据之外创建的Sqlite dabase文件与它不兼容。谢谢,但是[self-applicationDocumentsDirectory]stringByAppendingPathComponent似乎不存在,我正在使用XCode 4.3。另外,在执行代码中显示的操作之前,我是否需要先创建实体/属性?谢谢@hzxu该方法只是一个方便的方法,我刚刚编辑了我的问题以将其包括在内。@hzxu是的,您必须定义实体,请查看我发布的示例代码。
 /**
 Returns the path to the application's Documents directory.
*/
      - (NSString *)applicationDocumentsDirectory {
          return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) lastObject];
      }
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil)
    {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"yourSqlite.sqlite"];

    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"yourSqlite.sqlite" ofType:nil];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSError *error = nil;

    if  (![[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] ]) {

        if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] error:&error]){
            NSLog(@"Default file successfully copied over.");
        } else {
            NSLog(@"Error description-%@ \n", [error localizedDescription]);
            NSLog(@"Error reason-%@", [error localizedFailureReason]);
        }
    }

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}