3

私が尋ねた以前の質問 (これを参照) によると、アプリを配布した後に変更を加えて新しいデータを追加すると、テーブルの削除と再作成はうまく機能しません。データ。

これに対処するための推奨される戦略はありますか、および/またはこの問題を管理するために私がしなければならないことの例はありますか?

ありがとう

編集 :

こんなものでしょう

@Override
    public void onUpgrade (SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion){
        // TODO Auto-generated method stub
            //This is why I'm currently trying
        while(oldVersion < newVersion){
            switch(oldVersion){
            case 2:
                //add new datas from version 2 here
                break;
            case 3:
                //add new datas from version 3 here
                break;
            default:
                break;
            }
            oldVersion++;
        }
            /*
        try {
            Log.i(DatabaseHelper.class.getName(), "onUpgrade");
            TableUtils.dropTable(connectionSource, Category.class, true);
            TableUtils.dropTable(connectionSource, Level.class, true);
            TableUtils.dropTable(connectionSource, Hint.class, true);
            TableUtils.dropTable(connectionSource, HintCount.class, true);
            TableUtils.dropTable(connectionSource, Question.class, true);
            // after we drop the old databases, we create the new ones
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
            throw new RuntimeException(e);
        }
            */
    }
4

1 に答える 1

2

スキーマの変更ではなく、新しいデータのみが心配な場合はcreateIfNotExists、ORM lite のメソッドを試すことができます。したがって、基本的にすべてのアップグレードで、すべてのデータを挿入しようとします。ただし、新しいデータのみが挿入されます。

編集:
別のアプローチは次のとおりです。すべてのオブジェクトを、最初のオブジェクト、最初のアップグレードオブジェクト、2番目のアップグレードオブジェクトなどのグループに分割します。

ではonCreate、すべてのオブジェクトを作成し、すべてのグループを繰り返し処理します。ではonUpgrade、現在のバージョンに従ってグループを使用します。

于 2012-12-11T16:20:48.110 に答える