Reputation: 1638
I need to override the onDowngrade method, so that calls onDowngrade()
, whenever the older version of database in my app replaces the existing version of the app
i.e. (newer version db) < (older version db).
Example: When I tried to install the new app with the database version 3 will replace the current or already installed app with the database version 2, never called this onDowngrade method.
I hope my question is very clear. Please take a chance to give some idea about this method by answering this question.
MY new version of app Source code:
public class MyDatabase extends SQLiteOpenHelper
{
private static final int DB_VERSION = 10;
public MyDatabase(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
Log.d("Method","onCreate called");
.....
.....
.....
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.d("Method","onUpgrade called");
.....
.....
.....
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.d("Method","onDowngrade called");
.....
.....
.....
}
}
Manifest.xml
VersionCode: 10
MY old version of app Source code:
public class MyDatabase extends SQLiteOpenHelper
{
private static final int DB_VERSION = 9;
public MyDatabase(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
Log.d("Method","onCreate called");
.....
.....
.....
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.d("Method","onUpgrade called");
.....
.....
.....
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.d("Method","onDowngrade called");
.....
.....
.....
}
}
Manifest.xml
VersionCode: 9
Finally once again my query is I replaced the new app with the old app (which have corresponding source code as above).
But the Older version of app doesn't call the onDowngrade()
.
Upvotes: 2
Views: 1980
Reputation: 389
Its not an abstract method to override.
You can overload this method saying
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// what to do here
}
By default it will throw SQLiteException.
You have declared DB Version variable as static. So it will not be updated even when you redeclare it.
You have to reinitialize it some where in your launcher activity before initializing DB.
Also ensure min version in manifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
Upvotes: -1
Reputation: 1638
Found answer for this specific question.
in my AndroidManifest.xml file minSdkVersion is 8 as below and which never calls onDowngrade().
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
, but onDowngrade method will be called only when minSdkVersion must be greater than or equal to 11 as below.
Solution:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
Upvotes: 2