-1

I'm trying to upgrade my DB and everything is good, but the user_version won't upgrade, but every time I start the application. it would go to onUpgrade and perform this upgrading stuff again and again.

There is a similar question here onUpgrade database Android version number not increasing, but he haven't found any answer so I decided to ask once more, how to increment and save the db version?

I tried with setVersion, with PRAGMA, setting it outside the onUpgrade() and inside and everything that I could imagine, but it won't increment itself, my Database is 14MiB, so I can't afford to copy one more to assets as done the TS in the question above.

Here is my code for onUpgrade(), I'm exporting here the data of previous DB to XML, dropping the tables, creating new ones and importing data from the xml to new tables.

 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1) {
            cImportExport cImpExp = new cImportExport(db, cbActivity.MY_FILES_DIR + File.separator + "export.xml", oldVersion);
            cImpExp.Export();

            db.execSQL("DROP TABLE IF EXISTS " + "1");
            db.execSQL("DROP TABLE IF EXISTS " + "2");

            onCreate(db);
            cImpExp.Import();
        }
    }

UPD

I found out, that the version is successfully increments after I open the database and assign it to the variable:

  ps_dbHelper = new _DBHelper(this); 

        try {
            ps_db = ps_dbHelper.getWritableDatabase();
        } catch (SQLiteCantOpenDatabaseException ex) {
            alert(getString(R.string.db_not_found));
            onCreate(null);
        }

        int ver = ps_db.getVersion(); //IT'S = 2, AS IT SHOULD BE

The only question is why the db won't save this after closing? I tried to call .close methods for ps_db and ps_dbHelper explicitly to check it out, but it doesn't matter, after I start the app it'd go through onUpgrade with oldVersion=1..

UPD 2

The cImportExport initialization code:

public class cImportExport {
      private String mXmlFilePath = cbActivity.MY_FILES_DIR + File.separator + "exported.xml";
        private int mDbVersion = 0;
        private SQLiteDatabase mDataBase;

        public cImportExport(SQLiteDatabase db, String destXml, Integer dbVersion) {
            this.mDataBase = db;
            this.mXmlFilePath = destXml;
            this.mDbVersion = dbVersion;
        }

        public boolean Export() {
            cDbExport dbDump = new cDbExport(mDataBase, mXmlFilePath, mDbVersion);
            dbDump.exportData();
            return true;
        }
...

The cDbExport initialization code:

    class cDbExport {
        private String mDestXmlFilename;
        private SQLiteDatabase mDb;
        private Exporter mExporter;
        private int mDbVersion = 0;


        public cDbExport(SQLiteDatabase db, String destXml, Integer dbVersion) {
            mDb = db;
            mDestXmlFilename = destXml;
            mDbVersion = dbVersion;
...
Community
  • 1
  • 1
Movsar Bekaev
  • 888
  • 1
  • 12
  • 30
  • 1
    What do you mean by `user_version`? Also, are your table names really "1" and "2"? – AdamMc331 Sep 11 '15 at 17:01
  • in sqlite documentation this field is described as "user_version", they've said that there are also some "schema_version" that used by sqlite itself. No, my tables aren't named like that)) – Movsar Bekaev Sep 11 '15 at 17:38
  • 1
    The version is automatically set by `SQLiteOpenHelper` after `onUpgrade()` has returned successfully. Apparently, some of your code interferes with this. – CL. Sep 12 '15 at 08:16
  • @CL. can it be because I use one static instances of each throughout the application? Although I already tried with non-static and it has no effect(( – Movsar Bekaev Sep 12 '15 at 08:19
  • 1
    No, it must be some code you haven't shown. Does the exporter/importer always use the `db` parameter, or is there some code that uses its own connection? – CL. Sep 12 '15 at 08:42
  • @CL. I've added code of those classes (where vars do initialization), though I tried to run app without those methods, and still - it won't save the new version.. I can work around this by creating txt file with version, but it's very interesting to get to know why it is happening, and not just run from it)) – Movsar Bekaev Sep 12 '15 at 09:15
  • 1
    Is the code shown for `onUpgrade()` the actual code? Did you change anything? – CL. Sep 12 '15 at 11:42
  • @CL, no, it's actual code, but I found out what's the problem was, there was some function in my code out of view)) that function would copy the db from /data to external every time app starts, i removed it and now everything is good, thank you for support! – Movsar Bekaev Sep 12 '15 at 12:12

2 Answers2

1

You should never call setVersion

the right way to upgrade the version is change the parameter in the constructor of your SQLiteOpenHelper

MyDatabaseHelper(Context context) {
   super(context, "databasename", null, <this is the version>); 
}

The last parameter in the call to super is the version of your DB

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • http://stackoverflow.com/questions/11687473/android-upgrade-db-not-updating-db-version-after-successful-upgrade check this, do you have similar DatabaseHelper accessing same DB? – Derek Fung Sep 11 '15 at 17:50
  • I already use the constructor like that, and I didn't instantiate neither my database nor database helper more than once, this answer and this link is of no use whatsoever. – Movsar Bekaev Sep 12 '15 at 07:50
0

I found out that there was some function that would copy DB every time when app starts, so, the problem is solved.

Movsar Bekaev
  • 888
  • 1
  • 12
  • 30