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;
...