2

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().

Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • The example is one that calls onUpgrade(), downgrade is only when you install an older app version. – hgoebl Nov 11 '13 at 10:16
  • you mean onDowngrade is called only when I try to install older app version, but not the older database version alone? @hgoebl – Ashokchakravarthi Nagarajan Nov 11 '13 at 10:25
  • The sql-helper only compares the version in your code and the version in your database. Then it decides whether it's a downgrade or upgrade or nothing. BTW, how do you install an older database?! – hgoebl Nov 11 '13 at 10:28
  • @hgoebl Yes, I was wrong about installing older database, and I meant older app version which has older db version. Meanwhile I have updated my source code database and version as older. But still It never calls onDowngrade method. – Ashokchakravarthi Nagarajan Nov 11 '13 at 10:37
  • So what's the problem? Is your method not called or is there an exception? Give us information, show us the log-file... – hgoebl Nov 11 '13 at 10:40
  • My problem obviously is onDowngrade() is not called, whatever I tried to modify in my source code. – Ashokchakravarthi Nagarajan Nov 11 '13 at 10:45
  • We are not able to help you w/out more description and source code. And don't add further comments, but edit your question. – hgoebl Nov 11 '13 at 10:50
  • as your suggestion I have updated this question with source code @hgoebl ... – Ashokchakravarthi Nagarajan Nov 11 '13 at 11:08
  • The code looks good. I don't know why it doesn't work. Please read http://stackoverflow.com/questions/11365706/ondowngrade-in-sqliteopenhelper-or-another-way-to-not-crash-when-downgrading and the docs. – hgoebl Nov 11 '13 at 11:17
  • Guess I have done a small mistake, and would update after identified the mistake. Thank u very much for your help.. – Ashokchakravarthi Nagarajan Nov 11 '13 at 11:51

2 Answers2

2

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" />
-1

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" />
Suneel Prakash
  • 389
  • 5
  • 7