Update pre-loaded database with existing data Update pre-loaded database with existing data sqlite sqlite

Update pre-loaded database with existing data


In the newer versions, you should preserve the table data that you want to keep. To do this, you could:

  1. Make a copy of the old database file into a backup.
  2. Extract the newer database file to the working directory
  3. Open two SQLiteDatabase objects, one for each database file.
  4. Copy all data from the tables you want to preserve.
  5. Delete the backup of the old file if everything was successful.

For step 4 you shouldn't even need to code specifically, it can be done for every table, for example more or less like this (warning, this code is untested):

static void copyTable(SQLiteDatabase source, SQLiteDatabase destination, String tableName){    Cursor c = source.query(tableName, null, null, null, null, null, null);    destination.beginTransaction();    try    {        String[] columns = c.getColumnNames();        ContentValues insertValues = new ContentValues();        while (c.moveToNext())        {            insertValues.clear();            for (int i = 0; i < columns.length; i++)                insertValues.put(columns[i], c.getString(i));            destination.insert(tableName, null, insertValues);        }        destination.setTransactionSuccessful();    }    finally    {        destination.endTransaction();    }    c.close();}