In my app, I am supplying the most current version of my database via the assets folder. For simplicity’s sake, lets say my DB scheme is PRIM KEY INTEGER _id, TEXT name and INTEGER quantity. Quantity is the only thing that the user can edit in the app, everything else is static data. Quantity defaults at 0.
Here’s what I’m currently trying to do onUpgrade:
1.) Iterate through all of the tables, find the entries that have a quantity > 0, and store those entries in a list of objects that hold the ID and Quantity values, as well as the originating table name.
2.) Delete the old database that I just iterated through
3.) Copy the new database from assets that contains updated static information.
4.) Iterate through the list from step 1, and update the corresponding IDs with the quantities in the corresponding tables.
Here is my onUpdate method (titleArray is an array of strings containing my table names):
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion);
ArrayList<backupKey> bak = new ArrayList<backupKey>();
Cursor cursor = null;
for(int i = 0; i < titleArray.length; i++)
{
cursor = db.rawQuery("SELECT * FROM " + titleArray[i] + " WHERE quantity > 0", null);
if (cursor.moveToFirst()){
do{
backupKey key = new backupKey(titleArray[i],
cursor.getInt(cursor.getColumnIndex("_id")),
cursor.getInt(cursor.getColumnIndex("quantity")));
bak.add(key);
}while(cursor.moveToNext());
}
}
cursor.close();
myContext.deleteDatabase(DB_NAME);
try
{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName, false);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e)
{
Log.w("TaskDBAdapter", "failed");
throw new Error("Error copying database");
}
for (int i = 0; i < bak.size(); i++)
{
ContentValues args = new ContentValues();
args.put("quantity", bak.get(i).quantity);
String where = "_id = ?";
String[] whereArgs = {Integer.toString(bak.get(i).id)};
db.update(bak.get(i).range, args, where, whereArgs);
}
} //End of onUpgrade
I can confirm that the arraylist is successfully populated with the proper data. I was trying to just overwrite the database before, but it never seemed to work. I had to add myContext.deleteDatabase(DB_NAME); before the file writing operation for the database to be successfully replaced. I can confirm that my final update iteration isn’t generating any weird data– it has the correct values for the table name, id, and quantity.
The problem is, the quantities are not updated after my onUpgrade. The static data is updated to the latest version I have included in my assets folder, but the quantity data isn’t updated. Is this because I destroyed the database originally supplied to the method (the “db” parameter, in this case)? Is that even what the parameter is? How can I get the data updated in the new database?
Also: If there is a better way to go about this, I’m all ears =)
onUpgarde()is meant for updating database structure (adding/renaming columns, etc), not the actual data. You might want to do this in the actual app when starting for the first time. Additionally trying to upgrade user-entered data in place might not be the best idea. Why not have two tables (or databases even) — one with the data you are supplying and one with user entered data? You can simply copy rows users edit, and leave the original data read-only.