dex90
dex90

Reputation: 175

Android upgrading app without losing data

I have a problem, I want to upgrade my app, but I don't want to lose the data stored.

The problem is that in my database code I have this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + POINTS_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + NETWORKS_TABLE);
    onCreate(db);
}

I also thought of all sort of data backup, so I could at least save the data, and then to the upgrade. The problem is I think I just have two options:

a) root the device b) connect via usb

The problem is the device usb does not work. My computers does not seem to recognize it.

Is there anyway to save my data? I already tried applications Astro, Super Backup, Titanium Backup, but all of them requires root. Problem is, I can't root without an usb cable.

My tablet is an Samsung Galaxy Tab 2 7.0. Any tips?

Upvotes: 0

Views: 442

Answers (2)

bryan
bryan

Reputation: 808

That's the database version, not the app version. If your DB structure is changing, you can make a smarter upgrade path. An example from http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/ would be:

public void onUpgrade(
        final SQLiteDatabase db, final int oldVersion,
        final int newVersion)
    {
        int upgradeTo = oldVersion + 1;
        while (upgradeTo <= newVersion)
        {
            switch (upgradeTo)
            {
                case 5:
                    db.execSQL(SQLiteSet.V5_ADD_LAST_CARD);
                    db.execSQL(SQLiteCard.V5_ADD_FAILED);
                    break;
                case 6:
                    db.execSQL(SQLiteSet.V6_ADD_IMPORT_TYPE);
                    break;
                case 7:
                    db.execSQL(SQLiteSet.V7_ADD_SHORT_FNAME);
                    break;
            }
            upgradeTo++;
        }
    }

Upvotes: 0

Alexander
Alexander

Reputation: 48262

In onCreate you can do "CREATE TABLE IF NOT EXISTS"

In onUpdate you can do "ALTER TABLE" when needed

Upvotes: 3

Related Questions