Will
Will

Reputation: 1507

Rebuild SQLite database when app updates, Android

I need to rebuild the SQLite database in my app when users upgrade from the previous version as I have changed the content of the database, currently my app checks if it is the first run then builds the database so to save time on subsequent launches of the app. So when I update the app none of the new info in the database displays. Is there a way to check if the app is being updated instead of being installed for the first time? Here is my code for inserting data into the database:

      boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
      if (firstrun){
        new AlertDialog.Builder(this)
        .setTitle("Welcome!") //set the Title text
        .setMessage(...)
        .setNeutralButton("OK", null).show(); //Sets the button type
        Thread workThread=new Thread(new Runnable(){

            public void run() {
                dbHelper.deleteAllSpeakers(); // Clear database
                dbHelper.insertSomeSpeakers(); //Add some data
             runOnUiThread(new Runnable(){

                 @Override
                 public void run() {
                     mProgressDialog.dismiss();
                     Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG).show();
                     myFilter.setText("");
                 }

             });
         }

     });
     workThread.start();

     mProgressDialog=ProgressDialog.show(this, "Loading", "Building Speaker Database");

      }

Any help will be much appreciated.

Upvotes: 2

Views: 1594

Answers (2)

codeFood
codeFood

Reputation: 1251

It is a two step implementation.

  1. Implement onUpgrade().

  2. Change the version number of your DB.

The underlying implementation of SQLite on Android looks for an increase in version number for a call to onUpgrade() method.

Upvotes: 1

Sithu
Sithu

Reputation: 4431

You can use database version. Pass your database version by using SQLiteOpenHelper. You can refer to following answers.

How to update table schema after an app upgrade on Android?

How to upgrade SQLite database in Android?

Upvotes: 1

Related Questions