How to count total item after counting each category
I have some problem, in the logic. after I DISTINCT and COUNT the category, I will like to know the total. how can I do it ? this is what I wanna do, http://dl.dropbox.com/u/418769/2.png, and this is what I have done so far. 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
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @开发者_JAVA百科"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;
}
SELECT distinct category, COUNT(category) as count FROM drinks GROUP BY category
精彩评论