2011-07-31 37 views
0

我有一些問題,在邏輯中。在我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; 
    } 

回答

0

SELECT DISTINCT類,COUNT(類別)的數量從飲料GROUP BY類別

+0

斯科特您好,感謝您的答覆。該行並不是我想要的。我需要找出如何獲得總數並將其顯示在表格的第一行。 – Desmond

相關問題