ORMLite数据库更新

时间:2012-12-11 15:36:38

标签: java android database data-migration ormlite

根据我之前提出的问题(参见this),如果我进行更改并添加新数据,那么删除和重新创建表将无法正常工作 分发应用程序,因为用户将丢失所有数据。

是否有建议的策略来处理此问题和/或您是否有一些示例说明我必须采取哪些措施来解决此问题?

由于

编辑:

就像这样

@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);
        }
            */
    }

1 个答案:

答案 0 :(得分:2)

如果您只担心新数据,而不是架构修改,可以在ORM lite中尝试createIfNotExists方法。因此,基本上每次升级都会尝试插入所有数据。但是只会插入新数据。

修改
另一种方法可能如下: 将所有对象拆分为以下组:初始对象,第一个升级对象,第二个升级对象等。

onCreate中,您将创建所有对象,迭代所有组。在onUpgrade中,您将根据当前版本使用群组。