Android handle app updates and database changes
Asked Answered
L

1

13

I have basically finished developing an android app that makes use of SQLite databases that I copy to the user data area on the device eg /data/data/com.company.app/databases/users.db

I am unsure how the marketplace app update procedure takes place and am also unsure as to how I could test it.

I currently check whether the database exists on the device and copy it if it doesn't (generally only occurs on first ever launch). What happens if I have a new version of the database in my updated app? Will an marketplace update wipe user data so that it will copy my new database in on next launch?

What happens in the future if I make database changes/add records/etc and package this with the new app? Will this database not overwrite the old database?

Otherwise, to avoid copying and overriding the databases from the app bundle on every launch is there a way to check the size and date of the database files and only copy if the database in the bundle is newer?

If anyone needs clarification please ask.

Lectureship answered 14/10, 2010 at 2:28 Comment(0)
S
13

I am doing something similar. What I did is set the database version, and then when I check if the database exists, I also to make sure it's the correct version. If it's not, I save user favorites from the database, wipe and recopy my db, and then put back user favorites.

This is my on upgrade

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion == 2) {

        System.out.println("Performing upgrade!");
        openDataBase();
        // save the old favorites
        Cursor mCursor = getFavorites();
        ArrayList<Stop> favs = allCursorToStops(mCursor);
        mCursor.close();

        deleteRecreate(db);

        openDataBase();

        for (int i = 0; i < favs.size(); i++)
            setFavorite(favs.get(i));

        close();

    } else {

        deleteRecreate(db);

    }
}

Here is where I check existence/if need to upgrade etc

    boolean dbExist = checkDataBase();

    if(dbExist){
        // check if we need to upgrade
        openDataBase();
        int cVersion = myDataBase.getVersion();
        close();
        if(cVersion != VERSION)
            onUpgrade(myDataBase, myDataBase.getVersion(), VERSION);
Sieracki answered 24/12, 2010 at 17:37 Comment(2)
and how does onUpgrade(..) get called?Nippur
@powerX onUpgrade is called automatically when you increment the database version number.Hobnob

© 2022 - 2024 — McMap. All rights reserved.