In the onCreate()
method you must have the code that creates the tables as you want them to be in the latest version of the database.
So when you deployed the 1st version it would look like:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EMPLOYEE_TABLE = "CREATE TABLE " +
TABLE_EMPLOYEE + " ("+
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_EMPLOYEE_NAME + " TEXT)";
db.execSQL(CREATE_EMPLOYEE_TABLE);
}
but in the 2nd version, since you introduce a new column in the table it should be like:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_INPUT_TABLE = "CREATE TABLE " +
TABLE_EMPLOYEE + " ("+
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_EMPLOYEE_NAME + " TEXT, " +
COLUMN_EMPLOYEE_MOBILE + " TEXT)"; // new column
db.execSQL(CREATE_INPUT_TABLE);
}
The onCreate()
method will be called only if there is no database already installed.
So, when you deploy the 2nd version of the database, if the user has already installed the 1st version and the old database is still there, onCreate()
will not be called but onUpgrade()
will be called with the code that you have in your question and will alter the existing table.
If the user decides to uninstall the 1st version and then install the 2nd version, then onCreate()
will be called, because there is no database installed, and it will create the table(s) as you want them in that version.
Not that, onCreate()
or onUpgrade()
are not called automatically when the user installs the app.
They are invoked by the first call to getReadableDatabase()
or getWriteableDatabase()
.