Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade read-only database

I'm using SQLiteAssetHelper for importing ready databases into my app. Now I want to release a 2nd version of my database, but when users update the app, the database in not updating. I see documentation and saw I must use setForcedUpgrade(), but i don't know where to use it, I'll appreciate if you guide me. :)

MyDatabase.java :

public class MyDatabase extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 2;

public MyDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}}

And I read database with these:

    MyDatabase my;

    SQLiteDatabase sq;

    my = new MyDatabase(getApplicationContext());

    sq = my.getWritableDatabase();

Thanks by the way.

like image 273
user3456904 Avatar asked Aug 01 '26 13:08

user3456904


1 Answers

For your example:

    public class MyDatabase extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 2;

public MyDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

//FORCE UPGRADE BY DELETING OLD DATABASE
setForcedUpgrade(DATABASE_VERSION);

}}
like image 165
Nejc Draganjec Avatar answered Aug 04 '26 02:08

Nejc Draganjec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!