Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite database update

According to a previous question that I've asked (see this), dropping and re-creating tables is not going to work very well if I make changes and add new data once I've distributed the app, because users will lose all their data.

Is there a recommended strategy for dealing with this and/or do you have some examples of what I have to do to manage this problem ?

Thanks

EDIT :

It would be something like 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);
        }
            */
    }
like image 410
Alexis C. Avatar asked Feb 20 '26 16:02

Alexis C.


1 Answers

If you only worried about new data, not schema modification you can try createIfNotExists method in ORM lite. So basically on every upgrade you'll try to insert all data. But only new data will be inserted.

EDIT:
Another approach might be following: Split all you objects to groups like: initial objects, first upgrade object, second upgrade objects and so on.

In onCreate you'll create all objects, iterating on all groups. In onUpgrade you'll use groups according to current version.

like image 58
Mikita Belahlazau Avatar answered Feb 22 '26 07:02

Mikita Belahlazau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!