简体   繁体   English

如何使用onUpgrade()方法使用SQLiteAssetHelper升级数据库?

[英]how to upgrade my database using SQLiteAssetHelper using onUpgrade() method?

I am building a quiz app with preloaded database questions using the SQLiteAssetHelper class. 我正在使用SQLiteAssetHelper类构建包含预载数据库问题的测验应用程序。 my app is working fine with already 300 question quiz in the database. 我的应用程序可以正常运行,数据库中已有300个测验。 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! 我已经按照GitHub中SQLiteAssetHelper文档中的说明安装了SQLite比较,但是它为我生成了一个空的SQL文件,我不知道为什么! 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. 因此,我尝试了以下代码,并尝试仅插入两行用于测试数据库是否可以在我的android上从版本1升级到版本2,但不幸的是,这样做没有帮助。 I have looked everywhere in this form but the information related the SQLiteAsseHelper class is very few and not clear enough. 我已经以这种形式到处查看过,但是与SQLiteAsseHelper类相关的信息很少,并且不够清楚。 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);
}


}

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. 例如:在测验应用程序中更改问题1的答案,并通过运行应用程序检查值是否已更新。 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.ie, from lower version to latest version. 希望您知道只有在我们将app.ie从较低版本更新到最新版本时,才会调用onUpgrade方法。 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. 首先从Play商店下载并安装该应用,然后生成已签名的APK以获取最新版本(确保数据库版本已更新),然后以onupgrade方法举杯祝酒,并检查其是否符合条件。 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:) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM