データベース内の現在の情報を取得し、onUpgrade メソッドを使用して新しいバージョンに配置する関数を作成しようとしています。これは私が持っているものです。
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");
}
}
このメソッドは、onUpgrade メソッドに渡されたデータベースを受け取ります。
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (newVersion) {
case NEW_VERSION_NUMBER:
transTable.updateTable(db);
break;
}
}
問題は、onUpgrade メソッドから取得したデータベースに何もないことです! だから私は自分のデータベースを消去してしまいます....これを行う方法はありますか、それともALTERステートメントの限られた使用に頼らなければなりませんか. ありがとう。