计算每个类别后如何计算总项目数

时间:2011-07-31 07:06:57

标签: iphone ios xcode sqlite uitableview

我在逻辑方面有一些问题。在我DISTINCT和COUNT类别后,我想知道总数。我该怎么做 ?这就是我想做的事,http://dl.dropbox.com/u/418769/2.png,这就是我到目前为止所做的。 http://dl.dropbox.com/u/418769/5.png

Drink.m

 + (void) getCategory:(NSString *)dbPath {DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        //const char *sql = "SELECT DISTINCT category FROM drinks";
        const char *sql = "SELECT category, COUNT(category) FROM drinks GROUP BY category";
        sqlite3_stmt *selectstmt;
        if (sqlite3_prepare_v2(database, sql, -2, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                Drink *catObj = [[Drink alloc] initWithPrimaryKey:primaryKey];
                catObj.category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];   

                catObj.catCount = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];   

                NSLog(@"run here");
                catObj.isDirty = NO;
                [appDelegate.categoryArray addObject:catObj];
                [catObj release];
            }


        }


    } else sqlite3_close(database); //close db to release all memory
}

drinkCat.m

//自定义表格视图单元格的外观。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        // Set up the cell
        //Drink *drinkObj = [appDelegate.drinksArray objectAtIndex:indexPath.row];
        Drink *catObj = [appDelegate.categoryArray objectAtIndex:indexPath.row];
        //[drinkObj ];

        cell.textLabel.text = catObj.category;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.badgeString = catObj.catCount;//@"count";

        //[detailText release];

        return cell;
    }

1 个答案:

答案 0 :(得分:0)

SELECT distinct category,COUNT(category)as count FROM drink GROUP BY category