0

I am working on an Android application. As I upgrade the application, the database is re-created ie the onCreate() method of SQLiteOpenHelper class executes again. I have read that the app upgrade should not touch the database unless you increment the database version.

The database version remains same in the two versions, however, the application version changes.

Could anyone suggest me why my application is recreating the database upon app upgrade ?

Update 1:

My OnCreate() looks like this :

public override void OnCreate(SQLiteDatabase db)
{
    db.ExecSQL(DataContract.ENGINE_TYPE.CREATE_TABLE);
    db.ExecSQL(DataContract.ENGINE_GPS.CREATE_TABLE);

}

And I don't have anything in OnUpgrade() method.

And then I have code in the contract file that creates the table. The database is created perfectly and I can fetch the data using queries, but after the app upgrade (not database) all the existing data is lost. However, since I run the debug mode, so I can see OnCreate() method executing again.

Update 2:

DataContract class looks like this :

class DataContract{

    public class ENGINE_TYPE
        {
            public static string TABLE = "TypeInfo";
            public static string COLUMN_ENGINE_ID = "engine_id";
            public static string COLUMN_ENGINE_DESC = "engine_desc";

            public static string CREATE_TABLE = "CREATE TABLE " + TABLE +
                                            "(" + COLUMN_ENGINE_ID + " TEXT PRIMARY KEY " + " NOT NULL , " +
                                            COLUMN_ENGINE_DESC + " TEXT NOT NULL);";
    }

}

The other tables are also similar to this one.

Update 3:

My App Upgrade means only increasing the app version not database version.

I have deployed the application by creating APKs for the two versions. So, the upgrade is installing the latest version apk when a previous version apk already exists.

Next, I have also tried installing APK directly in deploy mode. Here also installing the latest version on top of an already installed app. So, I can see the program flow going through onCreate() method of SQLiteOpenHelper class in debug mode.

And I am working on Xamarin on Visual Studio

Puneet Chugh
  • 93
  • 10
  • I could make a suggestion if I could see some code. Try [edit]ing in some more details. Like the `onCreate` and `onUpgrade` methods of your DBHelper. – Matt Clark Nov 28 '16 at 21:34
  • Puneet, we can't guess what your code is doing. What does your DBHelper class look like? – MikeOscarEcho Nov 28 '16 at 21:34
  • @MattClark : I have updated my question – Puneet Chugh Nov 28 '16 at 21:52
  • @MikeOscarEcho : Please see the update in my question – Puneet Chugh Nov 28 '16 at 21:53
  • `public override void OnCreate(SQLiteDatabase db)` I have no idea what this is, but it's not valid. – Matt Clark Nov 28 '16 at 21:54
  • Please, again [edit] the question and post the **real** contents of your DBHelper. – Matt Clark Nov 28 '16 at 21:55
  • @MattClark It's valid C# code. This could be Xamarin – OneCricketeer Nov 28 '16 at 22:07
  • @MattClark : I have updated the content from DataContract file for creation of one of the tables. Android documentation suggests you to have a Contract class that defines all the constants and private classes for individual tables. Please find here https://developer.android.com/training/basics/data-storage/databases.html – Puneet Chugh Nov 28 '16 at 22:09
  • @cricket_007: Yes, its Xamarin code – Puneet Chugh Nov 28 '16 at 22:09
  • @MattClark : Can you suggest on the main point why the database is being created again ? – Puneet Chugh Nov 28 '16 at 22:11
  • 5
    Potential duplicate: http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run - What qualifies as an "Upgrade" to you? You should be tracking the internal `.sqlite` file that is created to ensure it's not being deleted at each deploy. – Jon Douglas Nov 28 '16 at 22:19
  • @JonDouglas : .sqlite is getting deleted at every deployment. I didn't upgrade the database version. What else can cause that ? Is there something in sdk 23 as I moved from a lower sdk to 23. – Puneet Chugh Dec 01 '16 at 21:50
  • Going back to my original question. What qualifies as an "Upgrade" process to you. Please list out the steps in your post. I'm not sure how you are deploying your application to device. Are you using an IDE? Are you installing and uninstalling an `.apk`? etc – Jon Douglas Dec 01 '16 at 22:12
  • @JonDouglas : I have updated my question – Puneet Chugh Dec 01 '16 at 22:32

0 Answers0