I am facing a very strange problem. I am developing an android application that has an sqlite DB as its database. I am using DBAdapter class which extends SQLiteOpenHelper. I have searched on internet but can't find a solution.
Here is my SQLiteOpenHelper class
public class DBAdapter extends SQLiteOpenHelper{
private static DBAdapter mInstance = null;
/**The Android's default system path of your application database. */
private static String DB_PATH = "/data/data/com.mydbapp.android/databases/";
private final static String DB_NAME = "mydb.sqlite";
private static final int DATABASE_VERSION = 1;
private static Context myContext;
public static DBAdapter getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new DBAdapter(ctx);
}
return mInstance;
}
private DBAdapter(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
DBAdapter.myContext = context;
DB_PATH = "/data/data/" +
context.getPackageName()+
"/databases/";
}
public void deleteDB()
{
myContext.deleteDatabase(DB_NAME);
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist )
{
this.getWritableDatabase();
}
if(!dbExist){
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Error copying database");
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (newVersion > oldVersion)
{
System.out.println("DB Upgrade logic")
}
}
Now when I change private static final int DATABASE_VERSION = 2;
then onUpgrade()
method doesn't get invoked, but when I change private static final int DATABASE_VERSION = 3;
then onUpgrade() works.
So my question is that why doesn't it invoke the onUpgrade()
method when I change DB version from 1 to 2.
Please help me resolve this issue.
Edit 1
I have noticed one more strange behavior. When I initially run application with DB version 1 and then again I install application with sane DB version (1) and now if I change DB version 1 to 2, then onUpgrade() is called. What I mean to say is that I have to install application 2 times with same DB version then if I change DB version, onUpgrade() is called.