How to update Android application for Android version 1 Native SQLite database without losing existing data

I need to update the Android app on the market to the next version. In the next version, I need to update the sqlite database without losing exsiting data.

In the first version, I did not create tables at runtime, but received the database file from the "assets" folder and copied it to the database path of my program. See This Link http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

In the next version of my application, I change the columns of the exsting table and add additional records, and there are also some new tables. I updated the database and copied it to the resource folder.

This will work for users who first buy the application, but not for existing users. My question is how can I update the database of one version of a version without losing existing data.

Sam.

+4
source share
4 answers
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); if(oldVersion == 2 && newVersion == 3) { db.execSQL("ALTER TABLE xyz ADD bobby int default 0"); } else { db.execSQL("DROP TABLE IF EXISTS xyz"); onCreate(db); } } } 
+3
source

Prepare an SQL query to update the database. If the database exists, then upgrade the remaining copy of the database from the assets.

In the tutorial you pointed out, there is a code like this:

  if(dbExist){ //do nothing - database already exist }else{ 

At the place where //do nothing - database already exist enter the update code.

+1
source

You can try the patch-like solution that I am describing in this blog post. This will help with a gradual upgrade and will continue to scale as you build more and more versions.

http://www.greenmoonsoftware.com/2012/02/sqlite-schema-migration-in-android/

+1
source

In your SQLiteHelper class, the DATABASE_VERSION variable should be the last. Suppose earlier DATABASE_VERSION was 1, and as it updates, it should be 2.

 DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } 

Now upgrade the version of the old database to the new version. If you do not set the latest version number in the database, then onUpgrade (..., ...) will be called again.

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { logger.info("DBManager - :::: Upgrading database from version " + oldVersion+ " to "+ newVersion + ", which will modify some old table"); String sqlFileName = "update_version_1.sql"; int totalSQLQueries = executeSQLScript(db, sqlFileName); logger.info("DBManager - :::: Upgrading database from version - " +"Total " + totalSQLQueries +" queries executed succesfully from " +sqlFileName); if(db.getVersion() == oldVersion) { db.setVersion(newVersion); logger.info("DBManager - :::: DB Version upgraded from " +oldVersion +" to " +newVersion); } } 

The change code for your database must be written inside the transaction. See the code below for an example of using a transaction to update a database: -

  private int executeSQLScript(SQLiteDatabase db, String sqlFileName) { int count = 0; InputStream inputStream = null; BufferedReader bufferReader = null; try { db.beginTransaction(); inputStream = localContext.getApplicationContext().getAssets().open(sqlFileName); bufferReader = new BufferedReader(new InputStreamReader(inputStream)); String inputLine; while((inputLine = bufferReader.readLine()) != null) { if(inputLine.trim().equalsIgnoreCase("BEGIN;") || inputLine.trim().equalsIgnoreCase("END;")) { continue; } else { db.execSQL(inputLine); count = count + 1; } } db.setTransactionSuccessful(); } catch(Exception ex) { ex.printStackTrace(); } finally { if(bufferReader != null) { try { bufferReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } db.endTransaction(); } return count; } 
0
source

All Articles