2017-04-11 85 views
-3

我是Android新手。我想在我現有的數據庫中添加一個已包含一些問題的列。我嘗試使用ALTER命令在onUpgrade()方法中設置一些代碼,但它不起作用。它給出了一個錯誤:where子句在sqlite數據庫中不起作用

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( 「這裏」,cursor.getCount()+ 「」)給出了在輸出中的0值。

+0

你有沒有增加DATABASE_VERSION代碼? – buzzingsilently

+0

在onUpgrade方法中,保存表的內容,刪除表。調用onCreate,然後添加保存的內容。 –

+0

是的,我增加了我的database_version。 –

回答

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