title: Insert data from text fields into SQLite database
body: <p>I am trying to insert data into my SQLite database. I have imported all the neccassry frameworks and database files nedded for my project. In my controller class xib there are two textfields and a button. I want the data entered into both of the text fields saved in my database when I click on the button. </p>
  
  <p>In my app delagate I have created two functions, one function to append the path of the database, and the other to insert data into the database. In the insert function, I check for certain conditions, i.e., if data gets added, an alert view should be displayed showing the record added, but when I add a new record it always goes into the <code>else</code> block, which is an error.</p>
  
  <pre><code>-(void)checkAndCreateDB {
      NSString* databaseName = @"login.sqlite";
      NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDir = [documentPaths objectAtIndex:0];
      NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"login.sqlite"];
      databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
      BOOL success;
      NSFileManager *fileManager = [NSFileManager defaultManager];
      success = [fileManager fileExistsAtPath:databasePath];
      if(success) return;
      NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
      [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
  }
  
  -(void)insertData{
  
      sqlite3 *database;
      sqlite3_stmt *statement;
       NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDir = [documentPaths objectAtIndex:0];
      NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"login.sqlite"];
      if(sqlite3_open([databasePath UTF8String], &amp;database) == SQLITE_OK)
      {
  
          NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO Loginchk (uname,password) VALUES ('%@',' %@');",Gunameq,Gpassq];
          const char *insert_stmt = [insertSQL UTF8String];
          if(sqlite3_prepare_v2(database, insert_stmt, -1, &amp;statement, nil)== SQLITE_OK)
          {
              sqlite3_bind_text(statement, 1, [Gunameq UTF8String], -1, NULL);
              sqlite3_bind_text(statement, 2, [Gpassq UTF8String], -1, NULL);
          }
  
          if(sqlite3_step(statement)==SQLITE_DONE) 
          {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
              [alert show];
              [alert release];
              alert=nil;
  
          }
          else 
          {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
              [alert show];
              [alert release];
              alert=nil;
          }   
          // Release the compiled statement from memory
          sqlite3_finalize(statement);    
          sqlite3_close(database);
      }
  }
  </code></pre>
  
  <p>This is my login controller class. Here I have declared a function through which I access my app delegate's functions.</p>
  
  <pre><code>-(IBAction)buttonPressed:(id)sender
  {
  
      Gpassq=Password.text;
      Gunameq=Uname.text;
  
  
      NSLog(@"%@%@",Gunameq,Gpassq);
      AddListAppDelegate *appDelegate =(AddListAppDelegate *)[[UIApplication sharedApplication]delegate];
      [appDelegate insertData];
  
  }
  </code></pre>
  
  <p>Please solve this problem.</p>