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:
- Make a copy of the old database file into a backup.
- Extract the newer database file to the working directory
- Open two
SQLiteDatabase
objects, one for each database file. - Copy all data from the tables you want to preserve.
- 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();}