title: Checking if a table exists and if it does not exist, create it... iOS/SQLite
body: <p>I currently have a table that exists which I'm able to pull data from.  What I would like to do is check if the table already exists in the bundle, and if it does not, I want to create the table and save it to the bundle (meaning the path would be in the main bundle).  I want the database to be checked and created at the top of the setInput method.  I've been scouring SO for something similar to this, but I haven't come up with anything yet.  Any help is very appreciated.  Here is my code:</p>
  
  <pre><code>-(IBAction)setInput:(id)sender
  {
      NSString *strStoreNumber;
      NSString *strRegNumber;
  
      strStoreNumber = StoreNumber.text;
      strRegNumber = RegNumber.text;
      lblStoreNumber.text = strStoreNumber;
      lblRegNumber.text = strRegNumber;
  
      NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];
  
      if(sqlite3_open([databasePath UTF8String], &amp;database) == SQLITE_OK) 
      {
          NSLog(@"Opened sqlite database at %@", databasePath);
          sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS tblStore (ID INTEGER PRIMARY KEY AUTOINCREMENT, Message TEXT)", NULL, NULL, NULL);
          //...stuff
      } 
      else 
      {
          NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
          sqlite3_close (database);
      }
  //    
      NSString *querystring;
  
      // create your statement
      querystring = [NSString stringWithFormat:@"SELECT strStore, strReg FROM tblStore WHERE strStore = %@ AND strReg = %@;", strStoreNumber, strRegNumber];  
  
      const char *sql = [querystring UTF8String];
  
      NSString *szStore = nil;
      NSString *szReg = nil;
  
      sqlite3_stmt *statement = nil;
      if (sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL)!=SQLITE_OK) //queryString = Statement
      {
          NSLog(@"sql problem occured with: %s", sql);
          NSLog(@"%s", sqlite3_errmsg(database));
      }
      else
      {
          // you could handle multiple rows here
          while (sqlite3_step(statement) == SQLITE_ROW) 
          {            
              szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
              szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
          }        
  
      }
  
      sqlite3_finalize(statement);
  
      lblStoreNumber.text = szStore;
      lblRegNumber.text = szReg;
  
  //   
  } 
  </code></pre>
  
  <p>I'm still quite new to iOS and SQLite, so if I did not provide an accurate enough description of what I'm trying to do, let me know and I'll try to be more specific.  Thanks!</p>