我正在开发一个Android学习应用程序,我有一个问题。我无法更新数据库中的问题。它只能显示数据库中的前五个问题。这是我的数据库代码,有人可以帮助我吗?感谢
package com.example.learningapp.test;
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Test";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_ans1= "Ans1"; //option a
private static final String KEY_ans2= "Ans2"; //option b
private static final String KEY_ans3= "Ans3"; //option c
private SQLiteDatabase dbase;
public DbHelper(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_ans1 +" TEXT, "
+KEY_ans2 +" TEXT, "+KEY_ans3+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1=new Question("Tumbuhan memerlukan cahaya matahari untuk" +
" _________________.","Bernafas", "Hidup", "Membuat Makanan", "Membuat Makanan");
this.addQuestion(q1);
Question q2=new Question("Manusia memerlukan empat keperluan asas, " +
"terdiri daripada", "Udara, Makanan, Cahaya Matahari, Air", "Air, Udara, Makanan, Tempat Perlindungan",
"Udara, Tempat Perlindungan, Cahaya Matahari, Air", "Air, Udara, Makanan, Tempat Perlindungan");
this.addQuestion(q2);
Question q3=new Question("Sisa-sisa makanan disingkirkan keluar melalui dubur" +
" sebagai tinja.Proses ini dinamakan ______________?","Penyahtinjaan", "Perkumuhan","Pernafasan","Penyahtinjaan");
this.addQuestion(q3);
Question q4=new Question("Semasa menarik nafas, dada akan _____________ dan _____________." +
" Pada masa yang sama, udara masuk ke peparu.", "naik, mengembang", "naik turun, menghempis", "naik, menghempis","naik, mengembang");
this.addQuestion(q4);
Question q5=new Question("Berikut adalah cara tumbuhan melindungi diri" +
" daripada musuh, KECUALI","Bergetah","Beracun","Daun jejarum","Daun jejarum");
this.addQuestion(q5);
Question q6= new Question("Pilihkan jawapan proses kitar hidup" +
"rama-rama yang betul.", "telur-> kepompong-> larva->rama-rama dewasa", "telur-> larva-> kepompong-> rama-rama dewasa",
"telur-> pupa-> larva -> rama-rama dewasa", "telur-> larva-> kepompong-> rama-rama dewasa");
this.addQuestion(q6);
Question q7= new Question("Pilihkan jawapan proses kitar hidup" +
"katak yang betul.", "telur-> berudu-> katak muda-> katak dewasa", "telur-> katak muda-> katak dewasa-> kepompong",
"telur-> pupa-> katak muda ->katak dewasa", "telur-> berudu-> katak muda-> katak dewasa");
this.addQuestion(q7);
}
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_ans1, quest.getans1());
values.put(KEY_ans2, quest.getans2());
values.put(KEY_ans3, quest.getans3());
// 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.setans1(cursor.getString(3));
quest.setans2(cursor.getString(4));
quest.setans3(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
答案 0 :(得分:0)
您要在onCreate
中添加问题。它在第一次开始时就被称为。您好像以后再添加了其他问题。您可以升级数据库或卸载应用程序并再次安装它以第二次触发数据库创建。无论如何,在onCreate
中填写您的值是不太好的做法。