在我的项目中,我使用CoreData来创建和使用数据库,它工作得很好。现在我有一个SQLite数据库文件(.db),我想将它集成到我的项目中并使用它(使用coredata)。如何使用Coredata添加和使用现有的SQLite数据库(x.db)?
我已经加入.db文件到我的项目,并创造了在使用下面的代码文件的目录就写副本 - :
- (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项目。
谢谢!