0

データベース内の現在の情報を取得し、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ステートメントの限られた使用に頼らなければなりませんか. ありがとう。

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 に答える