我有一个大型XML。如果我通过一条记录将它写入数据库,大概是2分钟。这个XML静态,永远不会更新。那么创建SQLite数据库并在Core Data中导入一个表可能会有用吗?我怎么能这样做?
我看到了这个教程(Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data),但没有说明如何导入表格
答案 0 :(得分:3)
从sqlite导入相当容易,但没有足够的文档记录恕我直言
使用FMDB就像:
NSManagedObjectContext *mom = ... ; //your mom
FMDatabase *db = [FMDatabase databaseWithPath:libraryDatabase];
if(![db open]) {
ddprintf(@"Failed to open database at %@", libraryDatabase);
return;
}
if(![db beginTransaction]) {
ddprintf(@"Failed to start Transaction to update database: %d, %@", db.lastErrorCode, db.lastErrorMessage);
[db close];
return;
}
id sql = @"SELECT modelId,imagePath,fileVolumeUuid FROM RKMaster";
FMResultSet *resultSet = [db executeQuery:sql];
while ([resultSet next]) {
//get row values
NSString *modelId = [row stringForColumn:@"modelId"];
NSString *orgFilename = [row stringForColumn:@"imagePath"];
//new MOC
MyEntity *entity = [mom insertNewObjectForEntity:@"myEntity"];
entity.modelId = modelId;
entity.orgFilename = orgFilename;
}
if(![db commit]) {
ddprintf(@"Failed to commit database transaction: %d,%@", [db lastErrorCode], [db lastErrorMessage]);
}
if(![db close]) {
ddprintf(@"Failed to propertly close database at %@", libraryDatabase);
}