java - 如何更新android中现有sqlite数据库中的数据

标签 java android sqlite

这是最初包含 21 个问题的数据库。我又添加了 10 个问题,但它们没有在数据库中更新。我如何将它们添加到此数据库中。我是 Android 新手,请解决这个问题。

 public class QuizHelper extends SQLiteOpenHelper {
 private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mathsone";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "qid";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; // correct option
private static final String KEY_OPTA = "opta"; // option a
private static final String KEY_OPTB = "optb"; // option b
private static final String KEY_OPTC = "optc"; // option c

private SQLiteDatabase dbase;

public QuizHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    dbase = db;
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
            + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
            + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT)";
    db.execSQL(sql);
    addQuestion();
    // db.close();
}

private void addQuestion() {
    Question q1 = new Question("Who is the president of india ?", "narender modi", "hamid ansari", "pranab mukherji", "pranab mukherji");
    this.addQuestion(q1);
    Question q2 = new Question(" Name of the first university of India  ?", "Nalanda University", "Takshshila University", "BHU", "Nalanda University");
    this.addQuestion(q2);
    Question q3 = new Question("Which college is awarded as Outstanding Engineering Institute North Award”?", "Thapar University", "G.N.D.E.C", "S.L.I.E.T", "G.N.D.E.C");
    this.addQuestion(q3);
    Question q4 = new Question("Name of the first Aircraft Carrier Indian Ship ?", "Samudragupt", "I.N.S. Vikrant", "I.N.S Virat", "I.N.S. Vikrant");
    this.addQuestion(q4);
    Question q5 = new Question("In which town of Punjab the largest grain market of Asia is Available?", "Bathinda", "Khanna", "Ludhiana", "Khanna");
    this.addQuestion(q5);
    Question q6 = new Question("Which is the highest dam in India?", "Bhakhra Dam", "Hirakud Dam", "Tehri Dam", "Tehri Dam");
    this.addQuestion(q6);
    Question q7 = new Question("Which Indian state is having longest coastline ?", "Rajasthan", "Gujrat", "Punjab", "Gujrat");
    this.addQuestion(q7);
    Question q8 = new Question("Name of the first Country to print books ?", "China", "India", "USA", "China");
    this.addQuestion(q8);
    Question q9 = new Question("Study of the Universe is known as?", "Sociology", "Cosmology", "Petology", "Cosmology");
    this.addQuestion(q9);
    Question q10 = new Question("Big Bang theory explains ?", "Origin of Universe.", "Origin of Sun", "Laws of physics.", "Origin of Universe.");
    this.addQuestion(q10);
    Question q11 = new Question("Which Planet is dwarf planet?", "Mercury", "Pluto", "Venus", "Pluto");
    this.addQuestion(q11);
    Question q12 = new Question(" In South Asia,the country with the largest percentage of aged population is ?", "India", "Sri Lanka", "Nepal", "Sri Lanka");
    this.addQuestion(q12);
    Question q13 = new Question("Which is the Largest lake of the world ?", "Caspian Sea ", "Dead Sea", "Arabian sea", "Caspian Sea ");
    this.addQuestion(q13);
    Question q14 = new Question("Which of following industry is famous of Jalandhar city?", "Cycle Parts Industry", "Electronics", "Sports Goods Manufacturing", "Sports Goods Manufacturing");
    this.addQuestion(q14);
    Question q15 = new Question("Capital of Punjab is:", "Ludhiana", "Amritsar", "Chandigarh", "Chandigarh");
    this.addQuestion(q15);
    Question q16 = new Question("How many  Seats of lok sabha are filled by the candidate of Punjab ?", "12", "17", "13", "13");
    this.addQuestion(q16);
    Question q17 = new Question("In Malwa and Doaba regions ____ river separates?", " Beas", "Jehlam", "Satluj", "Satluj");
    this.addQuestion(q17);
    Question q18 = new Question("Which one of the following city of Punjab State was known as Virat Ki Nagri?", "Amritsar", "Dasuha", "Ludhiana", "Dasuha");
    this.addQuestion(q18);
    Question q19 = new Question("Sachin Tendulkar scored his 100th international century against which’ country ?", "Bangladesh", "Australia", "West Indies", "Bangladesh");
    this.addQuestion(q19);
    Question q20 = new Question("What is the minimum permissible age for employment in any factory or mine?", "16", "13", "14", "14");
    this.addQuestion(q20);
    Question q21 = new Question("Where is India's First nuclear centre ?", "Rampur", "Mirapur", "Tarapur", "Tarapur");
    this.addQuestion(q21);
    Question q22= new Question("Srgsbhs","a","b","cd","cd");
    this.addQuestion(q22);
    Question q23= new Question("Srgsbhs","a","b","d","d");
    this.addQuestion(q23);
    Question q24= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q24);
    Question q25= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q25);
    Question q26= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q26);
    Question q27= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q27);
    Question q28= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q28);
    Question q29= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q29);
    Question q30= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q30);
    Question q31= new Question("Srgsbhs","a","b","f","f");
    this.addQuestion(q31);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}

// Adding new question
public void addQuestion(Question quest) {
    // SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_QUES, quest.getQUESTION());
    values.put(KEY_ANSWER, quest.getANSWER());
    values.put(KEY_OPTA, quest.getOPTA());
    values.put(KEY_OPTB, quest.getOPTB());
    values.put(KEY_OPTC, quest.getOPTC());

    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);
}

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            quest.setID(cursor.getInt(0));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(2));
            quest.setOPTA(cursor.getString(3));
            quest.setOPTB(cursor.getString(4));
            quest.setOPTC(cursor.getString(5));

            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}

}

最佳答案

在与 onCreate() 不同的位置调用 addQuestions() 或增加 DATABASE_VERSION 数字以强制数据库升级。

关于java - 如何更新android中现有sqlite数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171327/

相关文章:

java - 我怎样才能让它在谷歌地图上显示标记?

java - 二次方程求解器 nan 平方根正数

android - 如何在Android中获取联系人的电话号码

android - 给出 java.lang.NullPointerException : when i click on current location button

c# - Xamarin.iOS 上的 sqlite 中的缓慢 SELECT

android - 使用 OrmLite 在 Sqlite 中组合删除语句的帮助不大

java - 写数组作为参数Java

java - Java 中的复数求和函数

android - 显示 Instagram 缩略图的全屏图像

sqlite - 如何为 sqlite3.exe 提供密码?