Deepak
Deepak

Reputation: 177

struck in executing sqlite query using FMDB

I am using FMDB framework for SQLite in one of my iPhone project. The code is as follows:

for (int i = 0; i < CatID.count; i++) 
{ 
    NSString *subCatId = [NSString stringWithFormat:@"SELECT * FROM expense 
    where CAT_ID = %@ AND CURR_ID = %@ 
    AND EXP_DATE BETWEEN '%@' AND '%@' ",
    [CatID objectAtIndex:rowTrip],
    [currID objectAtIndex:1],
    _fromTextField.text,
    _toTextField.text];

    FMResultSet *result = [database executeQuery:subCatId];

    while([result next]) 
    {
        [_total addObject:[result stringForColumn:@"AMOUNT"]];
    }

}
  1. CatID is fixed i.e it is given by the user.

  2. carrID is fetched with for loop and is queried.

Every time after the SQL query, _total array is incremented (new object is added) but this is hanging.

Let's say we have "AMOUNT" output is 100, and CatID.count times objects are being added to _total array. Please tell me where i am doing wrong.

Upvotes: 2

Views: 629

Answers (1)

Ravi Raman
Ravi Raman

Reputation: 1070

While executing SQL queries, DONT use [NSString stringWithFormat:] method. Instead, just use the query as it is and pass arguments along the string in [database executeQuery:].

Try the following:

NSMutableArray* _total = [[NSMutableArray alloc] init];

for (int i = 0; i < [CatID count]; i++) 
{ 
    NSString *subCatId_QUERY = @"SELECT * FROM expense 
    where CAT_ID = %@ AND CURR_ID = %@ 
    AND EXP_DATE BETWEEN '%@' AND '%@' ";


    NSNumber* rowTrip  = [[CatID objectAtIndex:rowTrip] intValue];
    NSNumber* currID   = [[currID objectAtIndex:1] intValue];

    NSString* fromDate = _fromTextField.text;
    NSString* toDate   = _toTextField.text;

    FMResultSet *result = [database executeQuery:subCatId_QUERY,rowTrip,currID, fromDate, toDate];

    while([result next]) 
    {
        [_total addObject:[result stringForColumn:@"AMOUNT"]]; //are you sure about this? string??
    }

}

Upvotes: 2

Related Questions