I am building a quiz app with preloaded database questions using the SQLiteAssetHelper class. my app is working fine with already 300 question quiz in the database. but I can't figure out the way how to upgrade the database( adding new quiz questions to the table and NOT changing the structure or deleting any saved user data inside the old table). I have installed SQLite compare as described in the documentation of the SQLiteAssetHelper in GitHub but it generated an empty SQL file for me, I don't know why! so I tried the following code and tried to insert two rows only for testing if the database will upgrade on my android from version 1 to 2 but unfortunately, it didn't help. I have looked everywhere in this form but the information related the SQLiteAsseHelper class is very few and not clear enough. I am finishing my Quiz app these days and I am thinking if I will not be able to update the app it will be useless then.Please advise me on how to insert new rows to the old database without changing the previously existing rows because I save the user answer in the table.
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "quizMe_1.db";
private static final int DATABASE_VERSION = 2;
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2){
insertValues(db,"من اكثر فريق عالمي فوزا بكأس العالم ؟","فرنسا","ألمانيا","أيطاليا","البرازيل","0","4","0");
insertValues(db,"نبي سميت سورة من القران بأسمه ؟","عيسى","موسى","إبراهيم","نوح","0","3","0");
}
}
private static void insertValues(SQLiteDatabase db, String question,String ans1, String ans2, String ans3, String ans4,
String right, String userAsn, String field) {
ContentValues questionValues = new ContentValues();
questionValues.put("question", question);
questionValues.put("answer_1", ans1);
questionValues.put("answer_2", ans2);
questionValues.put("answer_3", ans3);
questionValues.put("answer_4", ans4);
questionValues.put("right_answer", right);
questionValues.put("user_answer", userAsn);
questionValues.put("field", field);
db.insert("quiz", null, questionValues);
}
}