I have an app that depends on SQLite for data which is populated by xmls shipped with the app in the assets folder.

When you run the app the first time it sets a shared preference config_run = false.

then i check if config_run = false then parse the xml and dump the data into db

set config_run = true

Now i realize that when i have to push an update on Google Play and add more content into the XML. Even though i change the database version from 1 to 2. The import script wont run because shared preference config_run value will be set to true.

Bugs Fixed and push and update but no data has changed - ver = 1.1, DB V = 1 (It should just replace the code and not upgrade or re-create the database)

Upgraded the DATA and pushed a new update - ver 1.2, DB = 2 ( No new code but data has to be re-created)

The Flow of My App

The App Starts Splash Activity. If Shared Pref - config_run is equal to false then it starts a Progress Dialog and parses and dumps the data into the database.

Upon Parsing and Creating DB and dumping data it goes to MainActivity.

Second Case

SplashActivity Runs and config_run = true so directly goes to MAin Activity.

As Suggested by few people below if i try to dumb the data into the database in onUpgrade of the SQLiteHelper it will happen only in MAinActivity as i dont open a Db connection in the SplashActivity and the Dialog Progress wont be displayed also.

5 Answers
5

Instead of setting your shared pref (config_run) to false and making it true, just set the database version into it. When you update your app, check whether you have the same version number in your shared pref. You can do this as shown below:

configRun = settings.getInt("database_version", 0);
if ((DBAdapter.DATABASE_VERSION) == configRun)
{
//skip xml parsing
}
else
{
//first time configRun will be "0" and DBAdapter.DATABASE_VERSION will be 1
// so you need to parse your xml here and set configRun =1
//on update, change your DB version to 2. now again your configRun and DBAdapter.DATABASE_VERSION will mismatch and you can parse your xml.
}

Why dont you want use built in sqlite versioning system. DB version is independed from app version. And it does exactly what you want. SQLiteOpenHelper? Every time tou change your db version an onUpgrate callback will be called and you can refill your db. There are a lot of examples.

you mean i should parse the xml from with in the sqlitehelper ? can u please point to any example ?
–
Harsha M VJan 19 '13 at 18:51

I mean that if you ship a db with your project there is a built in mechanizm to do db updates. onUpgrade method call when db version changes and you should do something to upgrage it. Why do you add xml to asset? Why you dont want ship prefilled db with app?
–
LeonidosJan 19 '13 at 19:41

yes i am using that. But using that with the shared pref is getting a bit complicated. I need to figure out a way only to dumb the data and recreate the data when i change content in my xml
–
Harsha M VJan 19 '13 at 19:43

So change db version in your sqlhelper when you change xml. public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } onUpgrade method will be called and you can drop old data and load new one.
–
LeonidosJan 19 '13 at 19:47

2

Yeah, if you open db (getWritableDatabase will be called some how), onUpgrade method will be called. It is up to you how to implement your db upgrade, but I prefer build in solutions. And SQLiteHelper has one.
–
LeonidosJan 21 '13 at 14:15

The one line changer: Instead of check for key="config_run", you check and set for key=("config_run"+DB_VERSION) to see if import is needed, and of course, if the key does not exist, you should return false.
This way every time you update the DB number, import job will run again.