java - Android update - merge database contents? -
what if update database tables app using, eg if introduce class or change existing. when user updates app, how can merge existing database content of app new db definitions?
there abstract method called onupgradedatabase(). function called when android thinks database needs updated. occurs when change database version number.
in recent projects i'm using following approach upgrade database on user's phone version version (even e.g: version1 version5 if user didnt update application while):
@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { updatedatabase(db, oldversion + 1, newversion); } private void updatedatabase(sqlitedatabase db, int fromversion, int toversion) { log.i("updating database version " + fromversion + " " + toversion); (int ver = fromversion; ver <= toversion; ver++) { switch (ver) { case 1: handledatabaseversion1(db); break; case 2: handledatabaseversion2(db); break; } } } private void handledatabaseversion1(sqlitedatabase db) { // create initial tables , on } private void handledatabaseversion2(sqlitedatabase db) { // here extend users table 2 columns } if modify schema including new tables, new columns, modified column types , whatsoever must execute proper sql statements update without deleting user's data! let's writing notepad application , notes stored in database. if modify notes table must not delete user's notes, handle data care.
Comments
Post a Comment