3

目前我正在为我的 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;
}
4

1 回答 1

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;
}
于 2013-01-21T09:37:28.417 回答