title: Multiple queries in SQLite for iOS
body: <p>Need help here.</p>
  
  <p>i managed to get the drinkName and categories working. <a href="http://dl.dropbox.com/u/418769/2.png" rel="nofollow">http://dl.dropbox.com/u/418769/2.png</a></p>
  
  <p>but i need to Distinct the categories and get a count value of the SQL database.</p>
  
  <p>something like this <a href="http://dl.dropbox.com/u/418769/2.png" rel="nofollow">http://dl.dropbox.com/u/418769/2.png</a> and then this <a href="http://dl.dropbox.com/u/418769/3.png" rel="nofollow">http://dl.dropbox.com/u/418769/3.png</a></p>
  
  <p>how would i need to do to get it done ?</p>
  
  <p>i need to run SELECT drinkID,drinkName from drinks, </p>
  
  <p>SELECT DISTINCT categories from drinks &amp; </p>
  
  <p>count each categoies's row.. can it be done ?</p>
  
  <p>this is my database looks like..http://dl.dropbox.com/u/418769/4.png</p>
  
  <p>i'm following this <a href="http://mybankofknowledge.wordpress.com/2010/12/01/accessing-sqlite-from-iphone-table-view/" rel="nofollow">http://mybankofknowledge.wordpress.com/2010/12/01/accessing-sqlite-from-iphone-table-view/</a></p>
  
  <p>ok it kind of work.....i on this error</p>
  
  <p><strong>* Terminating app due to uncaught exception 'NSRangeException', reason: '*</strong> -[NSMutableArray objectAtIndex:]: index 5 beyond bounds [0 .. 4]'</p>
  
  <ul>
  <li><p>(void) getInitialDataToDisplay:(NSString *)dbPath {
  DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate];</p>
  
  <p>if (sqlite3_open([dbPath UTF8String], &amp;database) == SQLITE_OK) {</p>
  
  <pre><code>const char *sql = "SELECT drinkID, drinkName FROM drinks";
  //const char *sql = "SELECT drinkName, categories FROM drinks";
  sqlite3_stmt *selectstmt;
  if (sqlite3_prepare_v2(database, sql, -1, &amp;selectstmt, NULL) == SQLITE_OK) {
  
  while(sqlite3_step(selectstmt) == SQLITE_ROW) 
      {
          NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
          Drink *drinkObj = [[Drink alloc] initWithPrimaryKey:primaryKey];
          drinkObj.drinkName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)]; 
          //drinkObj.categories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
  
          drinkObj.isDirty = NO;
          [appDelegate.drinksArray addObject:drinkObj];
          [drinkObj release];
      }
  
  
  }
  </code></pre>
  
  <p>} else sqlite3_close(database); //close db to release all memory
  }</p></li>
  <li><p>(void) getCategory:(NSString *)dbPath {
  DrinkTabsAndNavAppDelegate *appDelegate = (DrinkTabsAndNavAppDelegate *)[[UIApplication sharedApplication] delegate];</p>
  
  <p>if (sqlite3_open([dbPath UTF8String], &amp;database) == SQLITE_OK) {</p>
  
  <pre><code>const char *sql = "SELECT DISTINCT category FROM drinks";
  //const char *sql = "SELECT drinkName, categories FROM drinks";
  sqlite3_stmt *selectstmt;
  if (sqlite3_prepare_v2(database, sql, -1, &amp;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)];   
          //drinkObj.categories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
          NSLog(@"run here");
          catObj.isDirty = NO;
          [appDelegate.categoryArray addObject:catObj];
          [catObj release];
      }
  
  
  }
  </code></pre>
  
  <p>} else sqlite3_close(database); //close db to release all memory
  }</p></li>
  </ul>