使用FMDB执行sqlite查询时遇到了困难

时间:2012-11-28 10:04:30

标签: iphone ios xcode sqlite fmdb

我在我的一个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. 使用for循环获取
  3. carrID并进行查询。

  4. 每次在SQL查询之后,_total数组都会递增(添加新对象),但这是挂起的。

    假设我们"AMOUNT"输出为100,CatID.count次对象被添加到_total数组。请告诉我我在哪里做错了。

1 个答案:

答案 0 :(得分:2)

执行SQL查询时, DONT 使用[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??
    }

}