Java 为什么where子句查询在android中的sqlite数据库中不起作用?

Java 为什么where子句查询在android中的sqlite数据库中不起作用?,java,android,sqlite,android-database,Java,Android,Sqlite,Android Database,我使用OnUpgrade()方法在数据库“quest”中添加了一个名为id的列。但是当我运行应用程序时,arrayList中没有数据。我使用Log检查游标和ArrayList大小,两者都是零。当我第一次运行应用程序时,它给出了一个错误,没有一个名为id的列,之后它给出了一个错误: java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.chaitanya.myquiz/ c

我使用OnUpgrade()方法在数据库“quest”中添加了一个名为id的列。但是当我运行应用程序时,arrayList中没有数据。我使用Log检查游标和ArrayList大小,两者都是零。当我第一次运行应用程序时,它给出了一个错误,没有一个名为id的列,之后它给出了一个错误:

java.lang.RuntimeException: Unable to start activity  
   ComponentInfo
  {com.example.chaitanya.myquiz/
   com.example.chaitanya.myquiz.QuestionActivity}:
android.database.sqlite.SQLiteException: no such column: id (code 1):  
, while compiling: SELECT  * FROM quest where id = '1'
我试图重新安装应用程序,但在这一行中出现错误:

insert(TABLE_QUEST,null,value)

我多次检查查询。当我使用Select*from+TABLE\u QUEST;列表中有5个条目,可以正常工作,但在这种情况下不行。请帮助

public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
 // Database Name
 private static final String DATABASE_NAME = "bcd";
 // 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 static final String KEY_ID2 = "id";

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 " + KEY_ID2 + "    
        INTEGER)";
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",1);
this.addQuestion(q1);
Question q2 = new Question(" Name of the first university of India  ?", 
 "Nalanda University", "Takshshila University", "BHU", "Nalanda 
 University",1);
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",1);
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",1);
this.addQuestion(q4);
Question q5 = new Question("In which town of Punjab the largest grain 
market of Asia is Available?", "Bathinda", "Khanna", "Ludhiana", 
"Khanna",1);
this.addQuestion(q5);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    if (newV > oldV) {
        db.execSQL("ALTER TABLE " + TABLE_QUEST + " ADD COLUMN " +   
KEY_ID2 + " INTEGER DEFAULT 0");
    }
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());
values.put(KEY_ID2,quest.getID());
// 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 + " where " +    
KEY_ID2 + " = '1' ";
// + KEY_ID2 + " = 1"
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
Log.i("here",cursor.getCount()+"");
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        Question quest = new Question();
        quest.setID(cursor.getInt(0));
       // Log.i("here",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;
}

从表面上看,您的create查询似乎存在问题

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 " + KEY_ID2 + "    
        INTEGER)";

请注意,在
键ID2
之前的
文本之后没有逗号。纠正这一点。

现在当我使用quesList=db.getAllQuestions()时;和txtQuestion.setText(currentQ.getQUESTION());在另一个acitvity中,getQuestion方法出错。我检查的quelist大小为5(通过使用log.i)。错误:尝试对空对象引用调用虚拟方法“java.lang.String com.example.chaitanya.myquick.Question.getQuestion()
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 " + KEY_ID2 + "    
        INTEGER)";