How to keep existing database on app update?
<p>I have an iPhone app that use an Sqlite database to store some data and some user configurations. The problem that I'm having is that when I submit an update of my application, the existing database on the user installation  is overwrite with the empty database and the users lost their configurations. I'm sure it can not be too difficult to avoid this, but I don't know how to do it. </p>

<p>This is my code of the method that create the copy of the db:</p>

<pre><code>// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *writableDBPath = [self databasePath];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (!success) {
        // The writable database does not exist, so copy the default to the appropriate location.
        //NSLog(dbName);
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&amp;error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
}
</code></pre>

<p>This method is called form:</p>

<pre><code>- (BOOL)openDatabase {
    BOOL success = true;
    if (!database) {
        [self createEditableCopyOfDatabaseIfNeeded];
        if (sqlite3_open([[self databasePath] UTF8String], &amp;database) != SQLITE_OK) {
            success = false;
            // Even though the open failed, call close to properly clean up resources.
            sqlite3_close(database);
            NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        }
    }
    return success;
}


- (NSString*)databasePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:dbName];
    return path;
}
</code></pre>

<p>Maybe I forgot something in my code?</p>

<p>Can some one help me to solve this out? Thank you! </p>
