0

I have an app, where I use SQLite database, provided by me, so it is fixed and the user will not update anything in the database. Only results will be shown from that database.

The problem is - I am creating that database programatically ONLY on first run of the app:

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if(!prefs.getBoolean("firstTime", false)) {
        final DatabaseHelper myDb;
        myDb = new DatabaseHelper(this);
        myDb.deltab();
        myDb.insertData("Josh","Smith","012345678","something","something","something");myDb.insertData("Josh","Smith","012345678","something","something","something"); }

    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime",true);
    editor.apply();

First I drop the table (just to be sure it will create it from start) and then I create it and insert the data.

This is working fine on first app run.

But if I need to add some more data into that table in the future, or change any existing data in the next app version, how can I do that?

If the user downloads the new version of the app, the firstTime condition is not going to run, because he already started the old app before and by upgrading the version the sharedpreferences are not reset.

1 Answer 1

1

Looks like you're already using SQLiteOpenHelper to manage your databases (guessing on DatabaseHelper). That makes things easier. You can use it to support your needs.

  • Lose the SharedPreferences first run check as unnecessary. Just set up your database in your helper onCreate(). It gets run once if the database file did not exist in the device i.e. "first run".

  • To update from an old version to a new one, bump up the database version you're supplying to SQLiteOpenHelper constructor and do your data changes in onUpgrade(). It gets run if there was a database file with an older version number.

Sign up to request clarification or add additional context in comments.

2 Comments

So if I use only final DatabaseHelper myDb;myDb = new DatabaseHelper(this); it will automatically run onCreate() from DatabaseHelper on first run? And with the new app version, where should I call onUpgrade? Also it is better for me to delete the old dtb and create a new, not to write each change in the table, there can be more changes, it is easier to recreate the whole dtb in my case. Actually, my question is, where do I put the old and new version parameters in onUpgrade so this should run only once when the app is updated.
thank you. Am I correct, if I change the version number in my DatabaseHelper code - public DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, 1); }, the onUpgrade will be called automatically? If so, then I can have DROP table there and onCreate new and my issue is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.