如何将预先存在的sqlite文件导入到Core Data中?

我需要导入一个.sqlite文件到核心数据,我在互联网上search,发现:

核心数据教程:如何预加载/导入现有数据

它正在创build一个Python脚本来填充这个数据库,方法是读入旧数据库的内容,然后在新数据库中创build适当的行。 但是我的sqlite数据库在表和列的数量方面太大,这可能花费我相当多的时间。

我也发现这个:

在iPhone OS 3.0上使用预填充的SQLite数据库和核心数据

但我不太明白,它看起来像是将旧的数据库复制到一个新的数据库,那么它如何将Z_后缀添加到所有的表和列名? 此外,它要求我创build实体和属性,反正这是可以自动完成(从SQLite数据库文件)?

谢谢!

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

预填充核心数据

/** 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; }