我有超过 5000 条记录要从服务器加载到我的应用程序并插入数据库。在成功插入后插入这些记录时,我遇到了这种错误并且我的应用程序崩溃了。
数据库在 iPhone 中格式错误 + 错误磁盘 i/o 错误
以下是可能有用的代码片段,这里我一个一个地获取 5 个类别中的记录并将它们插入数据库
if (tag==1) {
NSMutableDictionary *catDic=[dic valueForKeyPath:@"GetAllRecords.results.categories.category"];
[self performSelectorInBackground:@selector(insertDataInCategoryMaster:) withObject:catDic];
NSMutableDictionary *levelDic=[dic valueForKeyPath:@"GetAllRecords.results.levels.level"];
[self performSelectorInBackground:@selector(insertDataInLevelMaster:) withObject:levelDic];
NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"];
[self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic];
[self getQueCatLevelData:2];
}
else if(tag==2){
NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"];
[self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic];
[self getQueCatLevelData:3];
}
else if(tag==3){
NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"];
[self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic];
[self getQueCatLevelData:4];
}//and so on for 5 also
在每个 insertDataInQueMaster 中,我都在做以下事情。
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
for(NSDictionary *queDictionary in dictionary)
{
NSString *strQuery = [NSString stringWithFormat:@"insert into QuestionMaster values(%d,'%@','%@',%d,%d,%d,%d,%d,0,'%@')",[[queDictionary valueForKey:@"questionid"]intValue], [queDictionary valueForKey:@"questiontext"],[queDictionary valueForKey:@"answertext"],[[queDictionary valueForKey:@"categoryid"]intValue],[[queDictionary valueForKey:@"levelid"] intValue],[[queDictionary valueForKey:@"status"]intValue],[[queDictionary valueForKey:@"RangeFrom"]intValue],[[queDictionary valueForKey:@"RangeTo"]intValue],[queDictionary valueForKey:@"Fact"]];
[NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
if(error)
{
NSLog(@"error description:%@",[[error userInfo] description]);
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"FirstTime"];
}
}
count++;
[[NSUserDefaults standardUserDefaults]setInteger:count forKey:@"CheckCount"];
[[NSUserDefaults standardUserDefaults]synchronize];
[pool drain];
我在 AppDelegate.m 中加载 App 时正在做这些所有事情,这也是在后台成功在数据库中插入几条记录后出现错误,所以我认为数据库打开和插入没有问题。
我希望我清楚,如果有任何困惑,请发表评论。等待回复。提前致谢。