【问题标题】:struck in executing sqlite query using FMDB在使用 FMDB 执行 sqlite 查询时受到打击
【发布时间】:2012-11-28 10:04:30
【问题描述】:

我在我的一个 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 数组。请告诉我我在哪里做错了。

【问题讨论】:

    标签: iphone ios xcode sqlite fmdb


    【解决方案1】:

    在执行 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??
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-23
      • 2011-04-19
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多