0

I would like to update my database and add a new table. So following this Stackoverflow question, I have incremented my database from version 1 to version 2. Then in onUpgrade, I wrote the code to add new table and insert data:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            String CREATE_B_TABLE = "CREATE TABLE IF NOT EXISTS b ( " +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "qset TEXT, "+
                    "highscore TEXT )";
            db.execSQL(CREATE_B_TABLE);

            String ADD_B = "INSERT INTO b ( qset ) VALUES ('1B'), ('2B'), ('3B'), ('4B'), ('5B'), ('6B'), ('7B'), ('8B'), ('9B'), ('10B')";
            db.execSQL(ADD_B);
        }
        this.onCreate(db);
    }

I also put the same code in onCreate to cater for new users.

However, something weird happened when the app is upgraded. The Add_B is inserted twice to the table, so I am having duplicated data. If I removed the code in onUpgrade, new table is added without duplication. It seem like onCreate is called when the app is upgraded, contradicting with the answer from another stackoverflow question.

user2872856
  • 2,003
  • 2
  • 21
  • 54

1 Answers1

0

If I read correctly the code you are intentionally calling the onCreate method from your onUpgrade.

Just before the method ends you call: this.onCreate(db)

sebasira
  • 1,739
  • 1
  • 22
  • 41
  • Thank you. What a silly mistake I made. I am trying to delete this question, but received warning from stackoverflow: "We do not recommend deleting questions with answers". Do you mind to delete your answer? – user2872856 Sep 02 '18 at 02:36
  • it won't let me delete it as well. Just "vote to delete it". I don't know how this voting system work. – sebasira Sep 02 '18 at 02:59