目前我正在为我的iPhone应用程序使用FMDB包装器。我的问题是,在执行2000个SQL插入语句(5000到8000个sql语句中)之后,我收到以下错误:
“错误14,无法打开数据库文件”
有人可以帮我解决此错误吗?
这是我的代码:
-(BOOL)insertOrUpdateinTable:(NSString *)tableName:(NSMutableArray *)columnName: (NSMutableArray *)columnValue {
[self initiateFMDB];
NSString *queryString=@"";
BOOL success;
//queryString =[[NSString alloc]init];
if (![db open]) {
NSLog(@"could not open db");
}
else {
//insert into Category(Categorycode,CategoryDesc) values (1,2)
queryString=[queryString stringByAppendingFormat:@"insert into %@(",tableName];
for (int cntCName=0; cntCName<[columnName count]; cntCName++) {
if (cntCName<[columnName count]-1){
queryString=[queryString stringByAppendingFormat:@"%@,",[columnName objectAtIndex:cntCName]];
}
else {
queryString=[queryString stringByAppendingFormat:@"%@",[columnName objectAtIndex:cntCName]];
}
}
queryString=[queryString stringByAppendingString:@") values ("];
for (int cntCValue=0; cntCValue<[columnValue count]; cntCValue++) {
if (cntCValue<[columnValue count]-1){
queryString=[queryString stringByAppendingFormat:@"'%@',",[columnValue objectAtIndex:cntCValue]];
}
else {
queryString=[queryString stringByAppendingFormat:@"'%@'",[columnValue objectAtIndex:cntCValue]];
}
}
queryString=[queryString stringByAppendingString:@")"];
// NSLog(@"QueryString=%@",queryString);
@try {
success=[db executeUpdate:[NSString stringWithFormat:@"%@",queryString]];
}
@catch (NSException *exception) {
NSLog(@"Exception error for selectFromItemPhoto is %@",[exception reason]);
}
}
if (success==TRUE) {
NSLog(@"Data inserted successfully");
}
else {
NSLog(@"Data is not inserted successfully");
}
// queryString=nil;
// [queryString release];
if ([db open]) {
[db close];
}
return success;
}
答案 0 :(得分:0)
我刚刚重新格式化了代码并删除了某些内容。尝试打印查询字符串,看看它是否有任何问题。
尝试以下方法:
-(BOOL)insertOrUpdateinTable:(NSString *)tableName withColumns:(NSMutableArray *)columnName withValues:(NSMutableArray *)columnValue
{
[self initiateFMDB];
NSString *queryString=@"";
BOOL success;
if (![db open]) {
NSLog(@"could not open db");
return NO; //no need to proceed if DB could be opened.
}
//insert into Category(Categorycode,CategoryDesc) values (1,2)
queryString=[queryString stringByAppendingFormat:@"insert into %@(",tableName];
for (int cntCName=0; cntCName<[columnName count]; cntCName++)
{
if (cntCName<[columnName count]-1)
queryString=[queryString stringByAppendingFormat:@"%@,",[columnName objectAtIndex:cntCName]];
else
queryString=[queryString stringByAppendingFormat:@"%@",[columnName objectAtIndex:cntCName]];
}//first for loop ends here
queryString=[queryString stringByAppendingString:@") values ("];
for (int cntCValue=0; cntCValue<[columnValue count]; cntCValue++)
{
if (cntCValue<[columnValue count]-1)
queryString=[queryString stringByAppendingFormat:@"'%@',",[columnValue objectAtIndex:cntCValue]];
else
queryString=[queryString stringByAppendingFormat:@"'%@'",[columnValue objectAtIndex:cntCValue]];
}//second for loop ends here
queryString=[queryString stringByAppendingString:@")"];
NSLog(@"QueryString=%@",queryString);
@try {
success=[db executeUpdate:[NSString stringWithFormat:@"%@",queryString]];
}
@catch (NSException *exception) {
NSLog(@"Exception error for selectFromItemPhoto is %@",[exception reason]);
}
if (success==YES)
NSLog(@"Data inserted successfully");
else
NSLog(@"Data is not inserted successfully");
[db close];
return success;
}