I'm trying to write a function that will take the current information in the database and place it into the new version with the onUpgrade method. This is what I have.
public void updateTable(SQLiteDatabase db) {
Cursor c = db.rawQuery(
"select DISTINCT tbl_name from sqlite_master where tbl_name = '"
+ tableName + "'", null);
if (c != null && c.getCount() > 0) {
c = db.rawQuery("select * from " + tableName, null);
ArrayList<Transaction> tempList = new ArrayList<Transaction>();
while (c.moveToNext()) {
Transaction t = createTransaction(c);
if (t != null)
tempList.add(t);
}
dropTable(db);
createTable(db);
for (Transaction t : tempList) {
addOccurrence(t);
}
} else {
throw new RuntimeException("Couldn't find new table for updating");
}
}
This method receives the database given to the onUpgrade method.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (newVersion) {
case NEW_VERSION_NUMBER:
transTable.updateTable(db);
break;
}
}
The problem is, nothing is in the database that I get from the onUpgrade method! So I just end up erasing my database.... Is there a way to do this or do I have to rely on the limited use of ALTER statements. Thanks.