java - How i update android app sql lite database on app update -


i have created android app have used local sql lite database. works fine. when updating version on play store , changing sql lite table app getting crashed. how update sql lite data base? local data important. how fix this?

if have modified database structure e.g. have added columns , new version of code utilises new columns have apply updates database.

the conventional way utilise onupgrade method if using subclass of sqliteopenhelper class. beware many examples drop table(s) , create new tables. e.g.

@override public void onupgrade(sqlitedatabase db, int oldversion, int   newversion) {     db.execsql("drop table if exists " + daily_stats_table);     oncreate(db); } 

note! not save data.

the onupgrade method invoked if database's version number increased (there ondowngrade method invoked if version number reduced.

e.g. may have code like;

    private static final int database_version = 1;     private static final string database_name = "statsmanager";      public stepsdbhelper(context context) {         super(context, database_name, null, database_version);     } 

in case changing private static final int database_version = 1; private static final int database_version = 2; result in onupgrade method being invoked (oldversion 1, newversion 2).

an alternative drop/create, newly added columns, alter table(s) e.g.

alter table add column columnname columntype 

you can set default value column(s) may required depending upon how app relies upon data held in column.

using alter instead of drop/create allows data in database preserved.

note! sqlite not support alter table drop column statement, removing columns. article discusses dropping columns how delete or add column in sqlite?

you should consider future potential database upgrades , include logic handles versions appropriately. onupgrade method has 2 parameters designed such logic passed when invoked, old version number (the current version of database) , new version number (the version database become).

e.g. might want include code like:-

if (oldversion == 1 && newversion == 2) {     //alter accordingly } if (oldversion ==2 && newversion == 3) {    //alter accordingly } ...... 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -