3

Currently I am using the FMDB wrapper for my iPhone app. My problem is that after executing 2000 SQL insert statements (out of 5000 to 8000 sql statements), I get the following error:

"error 14, unable to open database file"

Can anyone help me to resolve this error?

This is my code:

 -(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;
}
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Nikh1414
  • 1,238
  • 2
  • 19
  • 35
  • Not sure if this is your issue but you should look into just keeping the connection open and not open and close on every insert. – bryanmac Jan 19 '13 at 13:46
  • @bryanmacn COULD be the issue.. maybe internally FMDB runs out of io handles – Daij-Djan Jan 19 '13 at 14:01

1 Answers1

0

I have just reformatted your code and removed certain things. Try printing the query string, see if there is anything wrong with it.

Try the following:

-(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;
}
Ravi Raman
  • 1,070
  • 9
  • 16