0

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);
}


}
Krupa Kakkad
  • 857
  • 1
  • 13
  • 28
  • Not exactly an answer but I think u'll get an idea check this prepare similiar query and use that to upgrade https://stackoverflow.com/questions/13041062/android-sqlite-upgrade-without-losing-data – Raghavendra Aug 29 '17 at 12:36
  • What is the specific problem, other than "it didn't help"? Note that if you install a fresh application, it just installs the supplied database and does not invoke `onUpgrade()`. The bundled database must already be the up-to-date version. – laalto Aug 29 '17 at 13:08
  • Thas fine if I install the app to a fresh new user, becouse the app will preloaded with the fresh DB too. but what if the user already installed version 1 and his database included and saved his right and wrong answers for the quiz questions. in that senario app version 2 should only add the new quiz quesions to his database and not deleting or changing or adding any thing was previesly. – Sami Kassoum Aug 29 '17 at 14:03
  • Your `insertValues()` calls look OK. Don't they work? – CL. Aug 29 '17 at 14:04
  • the code seems to be not called . the onUpgrade() method looks like it has not been invoked . in a SqliteOpenHeplper class you call the onUpgrade in the onCreate() method , but here I have this sufisticated SqliteAssetHelper class that extends the SQLiteOpenHelper class – Sami Kassoum Aug 29 '17 at 14:33

1 Answers1

0

I am not sure whether we can update the database file which is in assets folder, but somehow I hope I can give you a idea to debug the solution. Before trying the below solution we need to make sure whether the values in the database file can be updated or not. For that update a value in the table by code and test it. For Example: Change the answer for question 1 in your quiz app and check whether the value is updated or not by running the app. If the value is updated then we can insert the new values also then try the below one, else we cannot update the values in database file.

Hope you were aware that onUpgrade mehtod will be called only when we update the app.i.e., from lower version to latest version. I am not sure how did you tested you code, if you have tested in the below way ignore it else try it. First download and install the app from the playstore, then generate the signed apk for latest version (make sure the database version is updated) install the apk put a toast in onupgrade method and check whether it get's into the condition. If the toast appears then check your insert code, else change the condition as below and give a try

if(newVersion == 2)

Hope this helpful:)

Jeevanandhan
  • 1,073
  • 10
  • 18
  • I am not trying to update any data in the previous DB version , I only update them when user is playing the quiz and aswering . if he is rigth then I write 1 in the the field "user_answer" in the specified quiz raw. I am looking for how to add rows or data or quiz questions to the database by releasing a new version. my app still not on the play store so I have to get a solution using android studio and my android phone only. – Sami Kassoum Aug 29 '17 at 14:11
  • yes I did Toast.makeText(cn,"onUpgrade called",Toast.LENGTH_SHORT).show(); it showed the message but didn't add the tow rows to the database – Sami Kassoum Aug 29 '17 at 14:41
  • @SamiKassoum:: Check whether the insert method is called or not? – Jeevanandhan Aug 29 '17 at 16:28
  • I have checked , it is called too. now I really don't know why the tow added rows won't appear in the database ? – Sami Kassoum Aug 29 '17 at 18:42
  • @SamiKassoum: As I suspect you cannot insert new values to your db file which is in assets folder.. https://stackoverflow.com/a/8700478/1501864 – Jeevanandhan Aug 30 '17 at 11:44