dimanche 22 février 2015

How to make onUpgrade() statements run even on first installation?

In my application i have a database which is created using DB queries in the DBHelper class. I want the onUpgrade statement to run on installation of the apk as there is a possiblity that the user might unistall and install the same application.


In this case it becomes the first installation on the device and the onUpgrade does not run which causes the application to crash.


It works perfectly if the new app is updated on the previous app.


How do i reslove this?? What is solution for such cases??


Please help !! Thanks in Advance !!


Here is my DbHelper.java



public class Dbhelper extends SQLiteOpenHelper {


public Dbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {

database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);


}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {

Log.e("","upgrade ");

database.execSQL("ALTER TABLE stock ADD db_stock_id varchar(100)");//1.1
//

}

public void dropandcreate(SQLiteDatabase database)
{

database.execSQL("DROP TABLE IF EXISTS image");
database.execSQL("DROP TABLE IF EXISTS attendance");
database.execSQL("DROP TABLE IF EXISTS stock");

onCreate(database);
}
}

Aucun commentaire:

Enregistrer un commentaire