在我的项目中,我使用 CoreData 创建和使用数据库,它工作得非常好。现在我有一个 SQLite 数据库文件(.db),我想将它集成到我的项目中并使用它(使用 coredata)。
我已将 .db 文件添加到我的项目中,并使用以下代码在 Documents 目录中创建了它的可写副本 -
- (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_METADATA',NSSQLiteErrorDomain=1}
请告诉我如何从这里开始。一些代码示例会很有帮助。
总而言之,我只想导入一个外部数据库并使用 CoreData 在我的 xCode 项目中使用它。
谢谢!