sqlite - What is the correct approach to upgrade Database in Android -
if want keep of old data , migrate new version of database , correct steps in onupgrade method?
- retain old data
- drop old tables
- create new tables , insert old data
is there simplified version or there better way? thanks!
upgrade database:
add new fields in table
do below steps in onupgrade
copy old data in temp table
create new table new fields
again copy data temp new table
drop temp table
also create new table in oncreate new users
sample code on upgrade
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { if (oldversion < newversion) { if (oldversion == 3) { /** * code upgrade privious version letest version * */ db.begintransaction(); db.execsql("alter table " + tableconstants.table_mst.gettablename() + " rename " + tableconstants.table_mst.gettablename() + "_obsolete"); db.execsql(table_create_query_mst); db.execsql("insert " + tableconstants.table_mst.gettablename() + "(" + "f1, f2, f3) " + " select f1, f2, f3 " + tableconstants.table_mst.gettablename() + "_obsolete"); db.execsql("drop table if exists " + tableconstants.table_mst.gettablename() + "_obsolete"); db.settransactionsuccessful(); db.endtransaction(); db.setversion(newversion); } } }
add new table in database
do below steps
in onupgrade 1. create new table
in oncreate 1. create new table
note: manage database version, same next version, check older version work or not.
Comments
Post a Comment