简体   繁体   English

使用 FMDB 执行 sqlite 查询

[英]struck in executing sqlite query using FMDB

I am using FMDB framework for SQLite in one of my iPhone project.我在我的 iPhone 项目之一中使用了 SQLite 的 FMDB 框架。 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 ie it is given by the user. CatID是固定的,即由用户指定。

  2. carrID is fetched with for loop and is queried. carrID使用 for 循环获取并进行查询。

Every time after the SQL query, _total array is incremented (new object is added) but this is hanging.每次在 SQL 查询之后, _total数组都会增加(添加新对象),但这会挂起。

Let's say we have "AMOUNT" output is 100, and CatID.count times objects are being added to _total array.假设我们的"AMOUNT"输出为 100,并且CatID.count次对象被添加到_total数组。 Please tell me where i am doing wrong.请告诉我哪里做错了。

While executing SQL queries, DONT use [NSString stringWithFormat:] method.在执行 SQL 查询时,不要使用[NSString stringWithFormat:]方法。 Instead, just use the query as it is and pass arguments along the string in [database executeQuery:] .相反,只需按原样使用查询并沿[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??
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM