-1

The app i did is in play . i fixed few bugs and now i want to make an update . I previously created tables in the past build.if the app encounters the lines where i create tables , will it ignore or should i do an explicit check whether table exists? using PreferenceVariables

if(preference_variables.contains("tables_created")) { 
    // dont create table
}  else {
    createTables();
}
Pete B.
  • 3,188
  • 6
  • 25
  • 38
user2749265
  • 245
  • 1
  • 4
  • 14

1 Answers1

0

Use the SQLiteOpenHelper class. It provides the necessary mechanics to create resp. upgrade your database tables depending on a database version, namley the onCreate and onUpgrade methods. See this SO entry for a good explanation.

detailed explanation

Imagine your app during its evolution from version 1.0 to e.g. 3.0.

So in version 1.0 you have say one table with some columns. The users download and run it, onCreate() gets called, you create the table and everything's fine.

Now you publish version 2.0 which has another table and adds some columns to the first table. Now you'll have two kinds of users. The ones upgrading from version 1.0 and new users which do not have version 1.0 but download and install version 2.0. Now for the existing users, onUpgrade() will be called, so here you put code to create the second table and alter the first one to add new columns. For new users, onCreate() is being called so you have to change that method to create both tables, the first and the second one.

Now you publish version 3.0 with say a third table. Now you'll have three kinds of users. The ones upgrading from version 2.0, the ones upgrading from version 1.0 (which did not install version 2.0) and new users. Again, for existing users onUpgrade() will be called. Now you have to make your upgrades depending on the values of oldVersion and newVersion. newVersion will be 3, oldVersion will be 1 for users upgrading from version 1.0 - so for those the upgrades from version 2.0 will be needed two, for users upgrading from version 2.0, oldVersion will be 2 so you only have to create your third table. And again, for new users onCreate() will be called and here you have to create all three tables now.

I hope this is enough of explanation.

Community
  • 1
  • 1
Ridcully
  • 23,362
  • 7
  • 71
  • 86