title: objective-c sqlite3 database changes not persistent
body: <p>I have an SQLite database for my app. To retrieve the entities from the db I use this method: </p>
  
  <pre><code>- (sqlite3*) openDatabaseNamed:(NSString*)databaseName
  {
      if(![databaseName isEqualToString:kTopInternationalDatabaseName] &amp;&amp;
         ![databaseName isEqualToString:kTop500RODatabaseName]){
          NSAssert(nil, @"Database does not exist!");
      }
      sqlite3 * dataBase;
      NSString * path;
      path =  [[NSBundle mainBundle] pathForResource:databaseName ofType:@"sqlite3"];
      if (sqlite3_open([path UTF8String], &amp;dataBase) != SQLITE_OK) {
          NSString * errorString = [NSString stringWithFormat:@"[SQLITE] Unable to open database &lt;%@&gt; ",databaseName];
          NSAssert(nil,errorString);
      }
      return dataBase;
  }
  
  - (NSArray *) getAllEntitiesForDatabaseNamed:(NSString*)databaseName
  {
    (...)
      sqlite3 * database  = [self openDatabaseNamed:databaseName];
  
      NSMutableArray *retval = [[NSMutableArray alloc] init];
      NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@",databaseName];
  
      NSArray *properties = [entityClass classProperties];
  
      if (sqlite3_prepare_v2(database, [query UTF8String], -1, &amp;statement, nil)
          == SQLITE_OK) {
          while (sqlite3_step(statement) == SQLITE_ROW) {
  
  
              for (int i=2; i&lt;countLimit; i++){
  
                  chars = (char *) sqlite3_column_text(statement,i+1);
                  if(chars != NULL)
                  {
                      containerString = [NSString stringWithUTF8String:chars];
                      if(containerString &amp;&amp; containerString.length&gt;0){
                          [entityModel setValue:containerString forKey:properties[i]];
                      }
                  }
              }
              [retval addObject:entityModel];
          }
          sqlite3_finalize(statement);
      }
      sqlite3_close(database);
      return retval.copy;
  }
  </code></pre>
  
  <p>Everything works as expected. For setting a custom field for an entity to a specific value in the database I use this method:</p>
  
  <pre><code>- (void)setEntity:(EntityModel *)entity favorite:(BOOL)favorite
  {
      NSString *query = [NSString stringWithFormat:@"UPDATE %@ SET favorite = %i WHERE position = '%i';",kDatabaseName,favorite?1:0,entity.positionInTop];
  
      sqlite3_stmt *statement;
      sqlite3 * database = [self openDatabaseNamed:kTop500RODatabaseName];
  
      if (sqlite3_prepare_v2(database, [query UTF8String], -1, &amp;statement, nil)
          == SQLITE_OK) {
          sqlite3_step(statement);
          sqlite3_finalize(statement);
      }
      sqlite3_close(database);
  }
  </code></pre>
  
  <p>What is happening is a little weird. If I use the update method and in the same lifecycle of the app query for all entities using <code>getAllEntitiesForDatabaseNamed</code> the changes I did with  <code>setEntity:Favorite:</code> persist. On the other hand, if I use the update method, then turn off the app and restart it, the changes I did using <code>setEntity:Favorite:</code> are lost. Any idea on why is that? </p>
  
  <p>PS: I also tried using <code>sqlite3_exec</code> and still the results are the same:</p>
  
  <pre><code>if (sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL) != SQLITE_OK) {
      // deal with error...
      NSLog(@" -- ERROR!");
  }
  </code></pre>