Amar Giram
Amar Giram

Reputation: 157

How to deal with SQLite database when updating the app on play store?

I have four tables in my SQlite database for previous version. Now i added the new table now my SQlite database have five tables what are the thinks should i do before i upload the apk on play store so my app won't be crash. I know on Upgrade() is there but i don't know how to deal with it correctly, please can any one tell me how to deal with this situation.

Upvotes: 1

Views: 1292

Answers (3)

TibiG
TibiG

Reputation: 849

This is how tour DatabaseHelper should look like.

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "mydb.db3";
private static DatabaseHelper sSingleton = null;
private final boolean mDatabaseOptimizationEnabled;

protected DatabaseHelper(Context context, String databaseName, boolean optimizationEnabled) {
    super(context, databaseName, null, DatabaseVersions.VERSION_NEW);
    mDatabaseOptimizationEnabled = optimizationEnabled;
}

public static synchronized DatabaseHelper getInstance(Context context) {
    if (sSingleton == null) {
        sSingleton = new DatabaseHelper(context, DATABASE_NAME, true);
    }
    return sSingleton;
}

@Override
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);

}

public SQLiteDatabase getDatabase(boolean writable) {
    return writable ? getWritableDatabase() : getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
    createDatabase(db, true, DatabaseVersions.VERSION_OLD, DatabaseVersions.VERSION_NEW);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion);

    switch (DatabaseVersions.VERSION_OLD) {
         case 1:
               //do here all the modifications that happened from db_version 1 to db_version 2 (alter tables, table creation, etc) notice no "break" after each case, so that if a user upgrades from 1 to 3, all the changes can happen for each sequential version.
         case 2:
               //do here all the modifications that happened from db_version 2 to db_version 3 (alter tables, table creation, etc)
    }

    createDatabase(db, false, oldVersion, newVersion);
}

private void createDatabase(SQLiteDatabase db, boolean freshDatabase, int oldVersion, int newVersion) {
    if (freshDatabase) {

        db.execSQL("CREATE TABLE " + Tables.STORIES + " (" +
                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                Stories.NAME + " TEXT," +
                Stories.STATUS + " TEXT," +
                Stories.DESCRIPTION + " TEXT," +
                Stories.CATEGORY + " TEXT," +
                Stories.ATTACHMENTS + " TEXT," +
                Stories.CREATION_DATE + " DATETIME," +
                Stories.PRICE + " TEXT" +
                ");");
    }

    if (mDatabaseOptimizationEnabled) {
        db.execSQL("ANALYZE;");
    }
}

public interface Tables {
    public static final String STORIES = "stories";
    public static final String LOG = "log";
}

Upvotes: -1

reixa
reixa

Reputation: 7011

For those who install the new version onCreate will be called and the new table is going to be created like the others.

If someone updates your app, onUpdate is going to be called. You should add the table creation code there and be aware of other versions you are handling.

override fun onUpgrade(db: SQLiteDatabase, oldV: Int, newV: Int) {
    if (oldVersion <  2) {
        upgradeVersion2(db)

    }
    if (oldVersion <  3) {
        upgradeVersion3(db)

    }
    if (oldVersion <  4) {
        upgradeVersion4(db)

    }
}

Here you have a link where Elye explains SQLite database migrations:

https://medium.com/@elye.project/android-sqlite-database-migration-b9ad47811d34

Upvotes: 1

VinceMedi
VinceMedi

Reputation: 210

Increment the versionCode in your build.gradle then override the onUpgrade method from SQLiteOpenHelper and put this :

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

    //update to v3 example considering the oldversion was 1 or 2
    if (oldVersion < 3) {
     //do your sql stuff here 
    }


}

Don't forget to add your new table in OnCreate method for devices which dl directly the new version.

Hope it helps !

Upvotes: 3

Related Questions