title: Display sqlite3 results in Table View using xcode
body: <p>I am an Objective C newbie (background includes perl/php/VB...non object oriented).  I am trying to learn Objective C and xcode and working on a project that involves data as a test case.  It uses story board.</p>
  
  <p>I have a database class, a class for single items and a class for multiple items and a view controller.  I have deleted the default scene and added a table view controller and selected the controller as it class.  I can get the data to dump in NSlog, but am stuck at getting it to display on the Table View.  Here is what I have (omitted are the single item h and m as well as the AppDelegate files):</p>
  
  <p>CarbDatabase.h:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "/usr/include/sqlite3.h"
  
  
  @interface CarbDatabase : NSObject {
      sqlite3 *_database;
  }
  
  
  + (CarbDatabase*)database;
  - (NSArray *)FoodItems;
  
  @end
  </code></pre>
  
  <p>CarbDatabase.m:</p>
  
  <pre><code>#import "FoodItem.h"
  #import "CarbDatabase.h"
  
  @implementation CarbDatabase
  
  static CarbDatabase *_database;
  
  + (CarbDatabase*)database {
      if (_database == nil) {
          _database = [[CarbDatabase alloc] init];
      }
      return _database;
  }
  
  - (id)init {
     if ((self = [super init])) {
      NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"sqlite3"];
  
         if (sqlite3_open([sqLiteDb UTF8String], &amp;_database) != SQLITE_OK) {
          NSLog(@"Failed to open database!");
              }
          }
      return self;
     }
  
  - (NSArray *)FoodItems {
  
  NSMutableArray *retval = [[NSMutableArray alloc] init];
  NSString *query = @"SELECT ID, LongDesc, ShortDesc, Carb, Sugar, lipid, GrmWt1, GrmWtDesc1, GrmWt2, GrmWtDesc2 FROM food ORDER BY LongDesc";
  
  sqlite3_stmt *statement;
  if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &amp;statement, nil)
      == SQLITE_OK) {
      while (sqlite3_step(statement) == SQLITE_ROW) {
          int uniqueID = sqlite3_column_int(statement, 0);
          char *LongDescChars = (char *) sqlite3_column_text(statement, 1);
          char *ShortDescChars = (char *) sqlite3_column_text(statement, 2);
          int Carb = sqlite3_column_int(statement, 3);
          int Sugar = sqlite3_column_int(statement, 4);
          int Lipid = sqlite3_column_int(statement, 5);
          int GrmWt1 = sqlite3_column_int(statement, 6);
          char *GrmWtDesc1Chars = (char *) sqlite3_column_text(statement, 7);
          int GrmWt2 = sqlite3_column_int(statement, 8);
          char *GrmWtDesc2Chars = (char *) sqlite3_column_text(statement, 9);
          NSString *LongDesc = [[NSString alloc] initWithUTF8String:LongDescChars];
          NSString *ShortDesc = [[NSString alloc] initWithUTF8String:ShortDescChars];
          NSString *GrmWtDesc1 = [[NSString alloc] initWithUTF8String:GrmWtDesc1Chars];
          NSString *GrmWtDesc2 = [[NSString alloc] initWithUTF8String:GrmWtDesc2Chars];
  
              FoodItem *info = [[FoodItem alloc] initWithuniqueID:uniqueID LongDesc:LongDesc ShortDesc:ShortDesc Carb:Carb Sugar:Sugar Lipid:Lipid GrmWt1:GrmWt1 GrmWtDesc1:GrmWtDesc1 GrmWt2:GrmWt2 GrmWtDesc2:GrmWtDesc2];
  
              [retval addObject:info];
          }
          sqlite3_finalize(statement);
      }
      else
      {
          NSLog (@"statement failed");
      }
      return retval;
  }
  
  @end
  </code></pre>
  
  <p>Controller.h:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface CarbViewController : UITableViewController
  
  @property (nonatomic, retain) NSArray *FoodItems;
  
  @end
  </code></pre>
  
  <p>Controller.m:</p>
  
  <pre><code>#import "CarbViewController.h"
  #import "CarbDatabase.h"
  #import "FoodItem.h"
  #import "/usr/include/sqlite3.h"
  
  @interface CarbViewController ()
  
  @end
  
  @implementation CarbViewController
  
  
  - (id)initWithStyle:(UITableViewStyle)style
  {
      self = [super initWithStyle:style];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
  
      self.FoodItems = [CarbDatabase database].FoodItems;
      self.title = @"Food Items %d",[_FoodItems count];
  
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
  }
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 0;
  }
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
  
      return [_FoodItems count];
  
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  
      static NSString *CellIdentifier = @"FoodItem";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                               if (cell == nil) {
                                   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
                               }
  
      // Configure the cell...
      int rowcount = indexPath.row;
      FoodItem *info = [_FoodItems objectAtIndex:indexPath.row];
      cell.textLabel.text = info.ShortDesc;
      cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Carbs per 100 Grams", info.Carb];
  
      return cell;
  }
  
  /*
  // Override to support conditional editing of the table view.
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Return NO if you do not want the specified item to be editable.
      return YES;
  }
  */
  
  /*
  // Override to support editing the table view.
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      if (editingStyle == UITableViewCellEditingStyleDelete) {
          // Delete the row from the data source
          [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
      }   
      else if (editingStyle == UITableViewCellEditingStyleInsert) {
          // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
      }   
  }
  */
  
  /*
  // Override to support rearranging the table view.
  - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  {
  }
  */
  
  /*
  // Override to support conditional rearranging of the table view.
  - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Return NO if you do not want the item to be re-orderable.
      return YES;
  }
  */
  
  #pragma mark - Table view delegate
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Navigation logic may go here. Create and push another view controller.
      /*
       &lt;#DetailViewController#&gt; *detailViewController = [[&lt;#DetailViewController#&gt; alloc] initWithNibName:@"&lt;#Nib name#&gt;" bundle:nil];
       // ...
       // Pass the selected object to the new view controller.
       [self.navigationController pushViewController:detailViewController animated:YES];
       */
  }
  
  @end
  </code></pre>