FMDatabase使用sum函数,ResultSet的bug

这个bug在SO上没搜到

sql语句是:

select sum(total) as total_sum, total, project_name from service_performance_day where enterprise_id = :eid and year = :year and month = :month and day = :day order by total desc

关键是使用了sqlite内置的sum函数,然后即使表中没有记录,ResultSet的next方法也返回YES

while([rs next]){
        
    double sum = [[rs objectForColumnName:@"total_sum"] doubleValue];
    double total = [[rs objectForColumnName:@"total"] doubleValue];
    NSString *name = [rs objectForColumnName:@"project_name"];
        
    ServicePerformance *performance = [[ServicePerformance alloc] initWithTitle:name Value:total Ratio:total / sum];
    [performances addObject:performance];
}

正常情况下,如果没有使用sum函数,不会走到while循环体中。但是使用了sum函数(其他内置函数没有测过),ResultSet的next函数就有BUG了

解决的办法是,先count一下符合条件的记录数

int count = [db intForQuery:@"select count(1) from service_performance_day where enterprise_id = :eid and year = :year and month = :month and day = :day;", enterpriseId, [NSNumber numberWithLong:year], [NSNumber numberWithLong:month], [NSNumber numberWithLong:day]];

if(count == 0){
    [db close];
    return [NSMutableArray array];
}



你可能感兴趣的:(ResultSet)