I'm developing a quiz-type application for android, and aiming to be able to expand the question bank over time. To do this, I'm pre-packaging the SQLite database, and checking for updates and overwriting it on upgrade:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion) {
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try {
inputStream = myContext.getAssets().open("questions.db");
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer))>0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch(IOException e) {
throw new Error("problem copying database from resource file");
}
}
}
What I'm unsure of is how to save whether a question has been attempted (and prevent that from being overwritten on upgrade). It seems my options are:
1) Save a boolean shared preference for every question id (potentially a LOT of sharedprefs)
2) Create a separate database to hold the data on which questions have been answered (seems like overkill)
3) Keep the results data in a separate table of the same database, and then ensure that only the questions table gets overwritten on upgrade
4) Keep the user data in its own column within the questions table, but prevent that column from being overwritten.
Of the above, 4 seems the most elegant (though not sure if possible). What is the best way of going about it (and if it's option 3 or 4, how do I go about modifying the code?!)
Much appreciated!