lundi 12 octobre 2015

Android : Change SQLite DB on upgrade

I want to add a column to my existing database

However I'm imagining I'm in the scenario where people have already got a version of the code and database and I'm going to make the changes through a update on google play, therefore previous data cannot be lost

To create my database I used the following code;

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_calculation);
  db = openOrCreateDatabase("EJuiceData", Context.MODE_PRIVATE, null);
  db.execSQL("create table if not exists Inventory(_id integer primary key autoincrement, NAME TEXT NOT NULL, AMOUNT TEXT, PRICE TEXT, AMOUNTLEFT TEXT, WEIGHT TEXT);");
....

I've looked around online and people are mentioning using the following command to update;

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  if (newVersion > oldVersion) {
    db.execSQL("ALTER TABLE Recipe ADD COLUMN NOTES TEXT");
  }
}

However onUpgrade is commented out stating the it isn't being called, also no where in my code have I stated what version of the database it is or given a new version

Anyone know how I get around this issue?

Thanks

Aucun commentaire:

Enregistrer un commentaire