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

时间:2017-04-13 08:03:33

标签: java android sqlite android-database

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

gulp-replace

我厌倦了重新安装应用程序,但后来我遇到错误:

dbase.insert(TABLE_QUEST,null,values);

我多次检查查询。当我使用Select * from + TABLE_QUEST;列表中有5个条目,它工作正常但不是这种情况。请帮助。

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'

这里是set get方法:

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

1 个答案:

答案 0 :(得分:1)

初步看来,您的创建查询似乎存在问题

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

观察TEXT之后KEY_ID2之后没有逗号。纠正这一点。