title: sqlite iPhone sdk help?
body: <p>This is the code I've written to access a SQLite database on the iPhone. The application is crashing for some reason.  Could any one help me with this?</p>
  
  <pre><code>-(void)openDB{
      //create database 
      if (sqlite3_open([[self filPath] UTF8String],&amp;db) != SQLITE_OK) {
          sqlite3_close(db);
          NSAssert(0, @"database failed to open");
      }
  }
  
  
  -(void)insertRecordIntoTableNamed:(NSString *)tableName withField1:(NSString *)field1 field1Value:(NSString*)field1Value andField2:(NSString *)field2 field2Value:(NSString *)field2Value {
  
      // [self insertRecordIntoTableNamed:@"Contacts" withFiled1:@"email" field1Value:email andField2:@"name" field2Value:name];
      NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@' , '%@') VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value];
      char *err;
      if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &amp;err)!=SQLITE_OK) {
          sqlite3_close(db);
          NSAssert(0,@"error updating table.");
      }
  }
  
  -(void)createTableNamed:(NSString *) tableName withField1:(NSString *)field1 withField2:(NSString *)field2 {
      char *err;
      NSString *sql = [NSString stringWithFormat:@"Creat table if not exist '%@' ('%@' text prmaru key,  '%@' TEXT",tableName,field1,field2];
      if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &amp;err) != SQLITE_OK) {
          sqlite3_close(db);
          NSAssert (0,@"tabel failed to create");
      }
  }       
  
  -(void)getAllRowsFromTableNamed:(NSString *)tableName{
      NSString *qsql = [NSString stringWithFormat:@"select * from %@",tableName];
      sqlite3_stmt *statment;
  
      if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &amp;statment, nil) == SQLITE_OK) {
          while (sqlite3_step(statment) == SQLITE_ROW) {
              char *field1 = (char *)sqlite3_column_text(statment, 0); 
              NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
              char *field2 = (char *)sqlite3_column_text(statment, 1);
              NSString *field2Str = [[NSString alloc] initWithUTF8String: field2]; 
              NSString *str = [[NSString alloc] initWithFormat:@"%@ - %@", field1Str, field2Str];
              NSLog(@"%@", str);
              //      [field1Str release];
              //      [field2Str release];
              //      [str release];
          }       
          sqlite3_finalize(statment);
      }
  }
  
  - (void)viewDidLoad {
      [self openDB];
      NSLog(@"%@",[self filPath]);
      [self createTableNamed:@"Contacts" withField1:@"email" withField2:@"name"];
  
      for (int i = 0; i&lt;= 2;i++) {
          NSString *email = [[NSString alloc]initWithFormat:@"user%i@yahoo.com",i];       
          NSString *name = [[NSString alloc]initWithFormat:@"user %i",i];
          [self insertRecordIntoTableNamed:@"Contacts" withField1:@"email" field1Value:email andField2:@"name" field2Value:name];
          [email release];
          [name release];
      }
  
      [self getAllRowsFromTableNamed:@"Contacts"];
      sqlite3_close(db);
      [super viewDidLoad];
  }
  </code></pre>