title: How to insert values using sqlite in iphone
body: <p>i am trying to insert values.But values are not saved.This is my coding.i alredy created the database using sqlite manager.That database name is "<code>feedback.sqlite</code>". If i run the code no errors will be displayed.But if i entered the data then click the save button the data will not be saved.if i run the code "Failed to open/create database" message will be displayed on the simulator.i cant guess where the error is.please give me an idea.thanks in advance.</p>
  
  <p><strong>Ratingviewcontroller.h</strong></p>
  
  <pre><code>@interface RatingViewController : UIViewController &lt;UITextFieldDelegate&gt; {
  
      sqlite3 *contactDB;
  
      IBOutlet UITextField *Traineeid;
      IBOutlet UITextField *Trainername;
      IBOutlet UITextField *Traineename;
      IBOutlet UITextField *Rating;
      IBOutlet UILabel *status;
  
      NSString *dbpath;
  }
  
  @property(nonatomic, retain) UITextField *Traineeid;
  @property(nonatomic, retain) UITextField *Trainername;
  @property(nonatomic, retain) UITextField *Traineename;
  @property(nonatomic, retain) UITextField *Rating;
  @property(nonatomic, retain) UILabel *status;
  
  - (IBAction)saveData:(id)sender;
  - (IBAction)findData:(id)sender;
  
  @end
  </code></pre>
  
  <p><strong>Ratingviewcontroller.m</strong></p>
  
  <pre><code>@implementation RatingViewController
  - (void)viewDidLoad {
  
      // Do any additional setup after loading the view, typically from a nib.
  
      NSString *docsDir;
      NSArray *dirPaths;
  
      // get the document directory
  
      dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,
                                                     NSUserDomainMask, YES);
  
      docsDir = [dirPaths objectAtIndex:0];
  
      // Build the path to the database file
  
      dbpath = [[NSString alloc] initWithString:
              [docsDir stringByAppendingPathComponent:@"feedback.sqlite"]];
      NSFileManager *filemgr = [NSFileManager defaultManager];
  
      if ([filemgr fileExistsAtPath:dbpath] == NO) {
  
          const char *db = [dbpath UTF8String];
  
          if (sqlite3_open(db, &amp;contactDB) == SQLITE_OK) {
  
              char *errMsg;
              const char *sql_stmt =
                  "CREATE TABLE IF NOT EXISTS CONTACTS (Traineeid INTEGER "
                  "PRIMARY KEY AUTOINCREMENT, Trainername TEXT, Traineename "
                  "TEXT, Rating float)";
  
              if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &amp;errMsg) !=
                  SQLITE_OK) {
  
                  status.text = @"Failed to create table";
              }
  
              sqlite3_close(contactDB);
  
          } else {
  
              status.text = @"Failed to open/create database";
          }
      }
  
      [super viewDidLoad];
  }
  
  - (void)saveData:(id)sender {
  
      sqlite3_stmt *statement;
      const char *database = [dbpath UTF8String];
  
      if (sqlite3_open(database, &amp;contactDB) == SQLITE_OK) {
  
          NSString *insertSQL =
              [NSString stringWithFormat:
                      @"INSERT INTO CONTACTS (Trainee id, Trainer name, Trainee "
                       "name,Rating) VALUES (\"%@\",\"%@\", \"%@\", \"%@\")",
                  Traineeid, Trainername.text, Trainername.text, Rating.text];
  
          /*  NSString *insertSQL = [NSString stringWithFormat:@"insert into
                                        CONTACTS
             (Traineeid,Trainername,Traineename,Rating) values
                                        (\"%d\",\"%@\", \"%@\",
             \"%f\")",[Traineeid integerValue],
                                         Trainername, Traineename,[Rating
             float]];*/
  
          const char *insert_stmt = [insertSQL UTF8String];
          sqlite3_prepare_v2(contactDB, insert_stmt, -1, &amp;statement, NULL);
  
          if (sqlite3_step(statement) == SQLITE_DONE) {
  
              status.text = @"Contact added";
              Traineeid.text = @"";
              Trainername.text = @"";
              Traineename.text = @"";
              Rating.text = @"";
  
          } else {
  
              status.text = @"Failed to add contact";
          }
  
          sqlite3_finalize(statement);
  
          sqlite3_close(contactDB);
      }
  }
  
  - (void)findContact {
  
      const char *datapath = [dbpath UTF8String];
      sqlite3_stmt *statement;
  
      if (sqlite3_open(datapath, &amp;contactDB) == SQLITE_OK) {
  
          NSString *querySQL = [NSString stringWithFormat:
                                    @"select Trainer name,Trainee name,Rating "
                                     "from CONTACT where Traineeid=\"%@\"",
                                Traineeid];
  
          const char *query_stmt = [querySQL UTF8String];
  
          if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &amp;statement, NULL) ==
              SQLITE_OK) {
  
              if (sqlite3_step(statement) == SQLITE_ROW) {
  
                  NSString *trainid = [[NSString alloc] initWithUTF8String:
                          (const char *)sqlite3_column_text(statement, 0)];
  
                  Traineeid.text = trainid;
  
                  NSString *trainernme = [[NSString alloc] initWithUTF8String:
                          (const char *)sqlite3_column_text(statement, 1)];
  
                  Trainername.text = trainernme;
  
                  NSString *traineenme = [[NSString alloc] initWithUTF8String:
                          (const char *)sqlite3_column_text(statement, 2)];
  
                  Traineename.text = traineenme;
  
                  NSString *rat = [[NSString alloc] initWithUTF8String:
                          (const char *)sqlite3_column_text(statement, 3)];
  
                  Rating.text = rat;
                  status.text = @"Match found";
  
              } else {
  
                  status.text = @"Match not found";
                  Traineeid.text = @"";
                  Trainername.text = @"";
                  Traineename.text = @"";
                  Rating.text = @"";
              }
  
              sqlite3_finalize(statement);
          }
  
          sqlite3_close(contactDB);
      }
  }
  
  - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  
      [textField resignFirstResponder];
      return YES;
  }
  @end
  </code></pre>