java - onUpgrade not called in sqlite -
i'm making update of app. in update, have add together field in table in sqlite db.
here file manage single instance of db:
databasemanager.java
public class databasemanager { private atomicinteger mopencounter = new atomicinteger(); private static databasemanager instance; private static sqliteopenhelper mdatabasehelper; private sqlitedatabase mdatabase; public static synchronized void initializeinstance(sqliteopenhelper helper) { if (instance == null) { instance = new databasemanager(); mdatabasehelper = helper; } } public static synchronized databasemanager getinstance() { if (instance == null) { throw new illegalstateexception( databasemanager.class.getsimplename() + " not initialized, phone call initializeinstance(..) method first."); } homecoming instance; } public synchronized sqlitedatabase opendatabase() { if (mopencounter.incrementandget() == 1) { // opening new database mdatabase = mdatabasehelper.getwritabledatabase(); } homecoming mdatabase; } public synchronized void closedatabase() { if (mopencounter.decrementandget() == 0) { // closing database mdatabase.close(); } }
then, main db file :
public class pdvhandler extends sqliteopenhelper { private static final string tag = "com.spmkt.mobile"; // static variables // database version public static final int database_version = 2; // database name public static final string database_name = "mydb.db"; .... public void dropandcreatemetrics() { sqlitedatabase db = databasemanager.getinstance().opendatabase(); db.execsql("drop table if exists " + metricshandler.table_metrics); db.execsql(metricshandler.create_table_metrics); databasemanager.getinstance().closedatabase(); } }
then, want add together column in alerta table :
alertahandler.java
public class alertahandler extends sqliteopenhelper { public alertahandler(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); databasemanager.initializeinstance(this); } public alertahandler(context context) { super(context, pdvhandler.database_name, null, pdvhandler.database_version); databasemanager.initializeinstance(this); } sqlitedatabase db; public static final string table_alertas = "alertas"; private static final string alerta_id = "id"; private static final string alerta_captura_id = "captura_id"; private static final string alerta_mensaje = "mensaje"; private static final string alerta_urgent = "urgent"; // column add together private static final string alerta_sync = "sincronizado"; // private static final string add_urgent_column = // "alter table "+table_alertas+" add together column "+ +" "; public static string create_alerta = "create table " + table_alertas + "(" + alerta_id + " integer " + "primary key autoincrement," + alerta_captura_id + " integer," + alerta_urgent + " integer default 0 not null, " + alerta_mensaje + " text" + ", " + alerta_sync + " integer )"; public static string drop_alerta = "drop table if exists '" + table_alertas+"'"; public arraylist<mensaje> getunsincronizedmessage() { arraylist<mensaje> lmensajes = new arraylist<mensaje>(); // select query mensaje message; string messagequery = "select * " + table_alertas + " " + alerta_sync + "= 0"; sqlitedatabase db = databasemanager.getinstance().opendatabase(); cursor cursor = db.rawquery(messagequery, null); // looping through rows , adding list if (cursor.movetofirst()) { { message = new mensaje(); message.setid(cursor.getint(cursor.getcolumnindex(alerta_id))); message.setcapturaid(cursor.getint(cursor .getcolumnindex(alerta_captura_id))); message.setmessage(cursor.getstring(cursor .getcolumnindex(alerta_mensaje))); int urgent = cursor .getint(cursor.getcolumnindex(alerta_urgent)); message.seturgent(urgent == 0 ? false : true); message.setsincronizado(cursor.getint(cursor .getcolumnindex(alerta_sync))); lmensajes.add(message); } while (cursor.movetonext()); } databasemanager.getinstance().closedatabase(); homecoming lmensajes; } public void addmessage(mensaje m) { sqlitedatabase db = databasemanager.getinstance().opendatabase(); contentvalues values = new contentvalues(); values.put(alerta_captura_id, m.getcapturaid()); values.put(alerta_mensaje, m.getmessage()); values.put(alerta_urgent, m.isurgent() ? 1 : 0); values.put(alerta_sync, m.getsincronizado()); // inserting row db.insert(table_alertas, null, values); databasemanager.getinstance().closedatabase(); } @override public void oncreate(sqlitedatabase db) { } public boolean existsmensaje(mensaje m) { sqlitedatabase db = databasemanager.getinstance().opendatabase(); string sql = "select count(*) " + table_alertas + " " + alerta_id + "=" + m.getid(); cursor mcount = db.rawquery(sql, null); mcount.movetofirst(); int count = mcount.getint(0); mcount.close(); databasemanager.getinstance().closedatabase(); if (count == 0) homecoming false; else homecoming true; } public void updatemessage(mensaje m) { sqlitedatabase db = databasemanager.getinstance().opendatabase(); contentvalues values = new contentvalues(); values.put(alerta_id, m.getid()); values.put(alerta_mensaje, m.getmessage()); values.put(alerta_sync, m.getsincronizado()); values.put(alerta_urgent, m.isurgent()); values.put(alerta_captura_id, m.getcapturaid()); // updating row db.update(table_alertas, values, alerta_id + " = ?", new string[] { string.valueof(m.getid()) }); databasemanager.getinstance().closedatabase(); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { int upgradeto = oldversion + 1; while (upgradeto <= newversion) { switch (upgradeto) { case 2: try{ db.execsql(drop_alerta); db.execsql(create_alerta); log.e(tag,drop_alerta); log.e(tag,create_alerta); }catch (exception e) { crashlytics.logexception(e); } break; } upgradeto++; } }
i read onupgrade run when getwritabledatabase runs, don't understand what's going on...
should alter tables in pdvhandler, because file define db version? should alter tables in each handler file? ( have 1 handler x table = 10 handlers)
i'm little bit lost ! suggestion appreciated !
should alter tables in pdvhandler, because file define db version? should alter tables in each handler file? ( have 1 handler x table = 10 handlers)
you should have 1 sqliteopenhelper
per database file.
onupgrade()
gets called when version of database schema request parameter supplied sqliteopenhelper
constructor larger schema version stored in database file. if database file upgraded requested version 1 sqlite helper working on same database file, onupgrade()
not invoked on helper.
also, should not grab exceptions in oncreate()
or onupgrade()
. if there's problem, exception should thrown outside method.
see also: when sqliteopenhelper oncreate() / onupgrade() run?
java android sqlite
No comments:
Post a Comment