我在我的一个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"]];
}
}
CatID
已修复,即由用户提供。
carrID
并进行查询。
每次在SQL查询之后,_total
数组都会递增(添加新对象),但这是挂起的。
假设我们"AMOUNT"
输出为100,CatID.count
次对象被添加到_total
数组。请告诉我我在哪里做错了。
答案 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??
}
}