Iphone 如何使用Coredata添加和使用现有的SQLite数据库(x.db)?
在我的项目中,我使用CoreData创建和使用数据库,它工作得非常好。现在我有了一个SQLite数据库文件(.db),我想将它集成到我的项目中并使用它(使用coredata) 我已将.db文件添加到我的项目中,并使用以下代码在Documents目录中创建了该文件的可写副本-:Iphone 如何使用Coredata添加和使用现有的SQLite数据库(x.db)?,iphone,ios,sqlite,core-data,Iphone,Ios,Sqlite,Core Data,在我的项目中,我使用CoreData创建和使用数据库,它工作得非常好。现在我有了一个SQLite数据库文件(.db),我想将它集成到我的项目中并使用它(使用coredata) 我已将.db文件添加到我的项目中,并使用以下代码在Documents目录中创建了该文件的可写副本-: - (void)createEditableCopyOfDatabaseIfNeeded { // First, test for existence. BOOL success; NSFileManager *fileM
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"wordlist1000.db"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"wordlist1000.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
现在,当我试图从数据库中获取结果时,我得到了这个错误
SQLite错误代码:1,“没有这样的表:Z_元数据”,NSSQLiteErrorDomain=1}
请告诉我如何从这里开始。一些代码示例将非常有用
总之,我只想导入一个外部数据库,并使用CoreData在我的xCode项目中使用它。
谢谢 核心数据在sqlite中存储数据的方式与您存储数据的方式不同。核心数据创建附加表以保存有关实体的信息。因此,sqlite db必须相同,以便在操作中不会出现任何问题
你不能那样做。核心数据不是SQLite的简单包装器。它提供了一个非常不同的API,并使用自己的、未记录的SQLite模式。SQLite是一个实现细节,而不是可以通过核心数据直接使用的东西 如果要将SQLite数据转换为核心数据,则需要: