0

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.

4

1 回答 1

0
public void onUpgrade(SQLiteDatabase new_db, int oldVersion,
                int newVersion) {
            // TODO Auto-generated method stub
            System.out.println("onUpgrade()");
            new_db.execSQL("DROP TABLE IF EXISTS " + TableName + ";");
            onCreate(new_db);
        }
于 2012-07-17T12:43:35.163 回答