Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the onUpgrade method ever called?

Is the onUpgrade method of SQLiteOpenHelper ever called? If so, when is it called and by what? If it is not called by the developers, then why is it there? What really happens with that function? I have seen examples where it drops all the tables, but then a comment says that dropping all the tables is not what you should do. Any suggestions?

like image 776
Mohit Deshpande Avatar asked Jul 02 '10 07:07

Mohit Deshpande


People also ask

What is onUpgrade?

onUpgrade is basically for handling new db changes(could be new columns addition,table addition) for any new version of your app. Droping the table is not always necessary in onUpgrade it all depends on what your use case is.

What is onUpgrade in sqlite?

onUpgrade method is called when the database version increases, not when the number of column is changed.

Which method must be overridden when we extend the SQLiteOpenHelper class?

SQLiteOpenHelper provides callback methods and we should override it to get our job done. Those callback methods that we can override are onCreate(), onUpgrade(), onOpen() and onDowngrade(). And onCreate() and onUpgrade() are abstract methods and must be overridden.


2 Answers

For those of you who would like to know the exact moment when onUpgrade() gets called, it is during a call to either getReadableDatabase() or getWriteableDatabase().

To those who are not clear how it ensure it gets triggered, the answer is: It is triggered when the database version provided to the constructor of SqLiteOpenHelper is updated. Here is a example

public class dbSchemaHelper extends SQLiteOpenHelper {  private String sql; private final String D_TAG = "FundExpense"; //update this to get onUpgrade() method of sqliteopenhelper class called static final int DB_VERSION = 2;  static final String DB_NAME = "fundExpenseManager";  public dbSchemaHelper(Context context) {     super(context, DB_NAME, null, DB_VERSION);     // TODO Auto-generated constructor stub } 

now to...onUpgrade()

@Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {     sql = "ALTER TABLE " + fundExpenseSchema.Expense.TABLE_NAME + " ADD COLUMN " + fundExpenseSchema.Expense.FUNDID + " INTEGER";     arg0.execSQL(sql); } 
like image 140
lazyList Avatar answered Sep 19 '22 15:09

lazyList


if your are using the SQLiteOpenHelper the onUpgrade will be called whenever you change the DB version. There is an additional requirement for this to work. The db name has to remain the same.

Old Version: dbName = "mydb.db" dbVersion = 1  New Version: dbName = "mydb.db" dbVersion = 2 

in the onCreate of the content provider you create an instance of the SQLiteOpenHelper that takes these params. Your SQLiteOpenHelper implementation would look like this:

public static final class MySQLiteOpenHelper extends SQLiteOpenHelper {          public MySQLiteOpenHelper(Context context, int dbVersion, String dbName) {             super(context, dbName, null, dbVersion);         }          @Override         public void onCreate(SQLiteDatabase db) {             //Code to create your db here         }          @Override         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {             // Code to upgrade your db here         }  } 
like image 25
dev.serghini Avatar answered Sep 21 '22 15:09

dev.serghini