where子句在sqlite数据库中不起作用

时间:2017-04-11 07:42:10

标签: java android sqlite android-sqlite

我是Android新手。我想在现有数据库中添加一个已包含一些问题的列。我尝试使用ALTER命令在onUpgrade()方法中设置一些代码,但它不起作用。这是一个错误:

ERROR:-   
       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'

这是代码。

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

}

Log.i(&#34; here&#34;,cursor.getCount()+&#34;&#34;)在输出中给出0值。

1 个答案:

答案 0 :(得分:0)

如果您只想在现有表quest中添加一个列,则无需从onCreate(db)方法调用onUpgrade()

修改onUpgrade()方法,如下所示:

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