title: UPDATE sql query when button pressed?
body: <p>So what I want to do is run an UPDATE query to my sqlite database when I press a button. Below is what I have and its not working, what I think is the problem is that I'm opening the database from the bundle, which of course is read only. Now I've been doing some research and I realized that I should:</p>
  
  <ol>
  <li><p>Check for the existence of the database in the Documents folder.</p></li>
  <li><p>If not there, copy the database from the bundle to the Documents folder.</p></li>
  <li><p>Now, open the database from the Documents folder.</p></li>
  </ol>
  
  <p>But I'm don't know how to do that??? Any help would be greatly appreciated. Here's what I have: </p>
  
  <pre><code>- (IBAction)update:(id)sender {
  @try {
      NSFileManager *fileMgr = [NSFileManager defaultManager];
      NSString *dbPath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
      BOOL success = [fileMgr fileExistsAtPath:dbPath];
      if(!success)
      {
          NSLog(@"Cannot locate database file '%@'.", dbPath);
      }
      if(!(sqlite3_open([dbPath UTF8String], &amp;db) == SQLITE_OK))
      {
          NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
  
      }
      const char *sql = "UPDATE strengthexercises SET isFavorite = 'true' WHERE ID = '1'";
      sqlite3_stmt *sqlStatement;
      if(sqlite3_prepare(db, sql, -1, &amp;sqlStatement, NULL) != SQLITE_OK)
      {
          NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
      }else{
          if (sqlite3_step(sqlStatement) == SQLITE_DONE){
              NSLog(@"Success");
          }
      }
      sqlite3_finalize(sqlStatement);
  }
  @catch (NSException *exception) {
      NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
  }
  @finally {
      sqlite3_close(db);
  }
  
  }
  </code></pre>
  
  <p>A clear answer with an example would help a lot. Thanks in advance.</p>
  
  <p><strong>Update #1</strong></p>
  
  <p>Now I have this, but it still dose not work...</p>
  
  <pre><code>- (NSString *)applicationDocumentsDirectory {
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil;
  return basePath;
  }
  
   - (IBAction)update:(id)sender {
  @try {
      NSString *docsPath = [self applicationDocumentsDirectory];
      NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
      BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
      if (!fileExists) {
          // get the source path to copy from
          NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
          // copy db to documents
          [[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil];
      }
      if(!(sqlite3_open([dbPath UTF8String], &amp;db) == SQLITE_OK))
      {
          NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
      }
      const char *sql = "UPDATE strengthexercises SET isFavorite = 'true' WHERE ID = '1'";
      sqlite3_stmt *sqlStatement;
      if(sqlite3_prepare(db, sql, -1, &amp;sqlStatement, NULL) != SQLITE_OK)
      {
          NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
      }else{
          if (sqlite3_step(sqlStatement) == SQLITE_DONE){
              NSLog(@"Success");
          }
      }
      sqlite3_finalize(sqlStatement);
  }
  @catch (NSException *exception) {
      NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
  }
  @finally {
      sqlite3_close(db);
  }
  
  }
  </code></pre>