2

我在我的一个 iPhone 项目中使用 SQLite 的 FMDB 框架。代码如下:

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是固定的,即它是由用户给定的。

  2. carrID使用 for 循环获取并进行查询。

每次 SQL 查询后,_total数组都会增加(添加新对象),但这会挂起。

假设我们的"AMOUNT"输出是 100,并且CatID.count时间对象被添加到_total数组中。请告诉我我在哪里做错了。

4

1 回答 1

2

在执行 SQL 查询时,不要使用[NSString stringWithFormat:]方法。相反,只需按原样使用查询并在[database executeQuery:].

尝试以下操作:

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??
    }

}
于 2012-11-28T11:45:20.607 回答