0

In the last time I get some strange reviews on the PlayStore.

enter image description here

Whyever after installing the update of my app, users complain all their app data has been wiped. I have absolutely no idea how this can happen. Database is only recreated if tables do not exist.

Code which is executed after opening database connection:

private void createTablesIfNotExist() {
    //query tables
    Cursor c = DatabaseManager.executeSelect("SELECT name FROM sqlite_master WHERE type = \"table\"");

    //if tables already created, do nothing (+1 because of header table)
    if (!((tableAmount + 1) == c.getCount())) {
        //meta data table
        this.database.execSQL("DROP TABLE IF EXISTS android_metadata;");
        this.database.execSQL("CREATE TABLE android_metadata (locale TEXT);");
        this.database.execSQL("INSERT INTO android_metadata VALUES('de_DE');");
    }

    //create event table
    c = DatabaseManager.executeSelect("SELECT name FROM sqlite_master WHERE name = \"" + EVENTS + "\"");
    c.moveToFirst();

    if(c.isAfterLast()) {
        this.database.execSQL("CREATE TABLE.....");
    }

    c = DatabaseManager.executeSelect("SELECT name FROM sqlite_master WHERE name = \"" + ASSIGNED_PRODUCTS + "\"");
    c.moveToFirst();

    if(c.isAfterLast()) {
        this.database.execSQL("CREATE TABLE.......");
    }

}

Has anyone an idea? Thanks :)

maximilian
  • 35
  • 7

1 Answers1

1

I think you don't "rewrite" your data from old database to new one.

Android: upgrading DB version and adding new table

Check this link and if it is your problem then if possible you can transfer data for each version to new one and everything will be fine. You have to remember to design database and dataflow to keep in mind that old users can lack some data, so some data in new database can be nullable

Janusz Hain
  • 597
  • 5
  • 18
  • Thanks. The thing is, my approach does not create a new database, but checks if all neccessary tables are available. If not, they are created (see code). – maximilian Aug 13 '17 at 19:45
  • Did you create SQLiteOpenHelper implementation? Your implementation seems custom and this is harder to maintain this way. I would rework database by creating SQLiteOpenHelper implementation. Ofc all clients will again lose DB, but from now on you will have much less work maintaining db – Janusz Hain Aug 14 '17 at 05:04
  • 1
    Janusz is right. Android best practices would have you use the SqliteOpenHelper class. – Bill Mote Aug 14 '17 at 11:44