2016-03-05 92 views
-1

我已經瀏覽了幾個關於列不存在錯誤的問題,大多數都是間距問題或與模式有關的問題,這可以通過修復卸載並重新安裝應用程序。我校對了我的聲明,並嘗試從我的手機中卸載應用程序,然後從android studio重新下載。似乎也沒有解決我的問題。SQLite數據庫列不存在

這裏該行引發錯誤:

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + DATE_TO_REMIND + "='" + date + "'", null); 

它的意圖是把表中的每個元素(其列同行),有這是一個參數,我想要的日期( 。我有方法(getWordsToReview)

我不斷收到錯誤是這樣的:

Caused by: android.database.sqlite.SQLiteException: no such column: Date_To_Remind (code 1): , while compiling: SELECT * FROM Vocabulary WHERE Date_To_Remind='2016/03/05'

該類的其餘部分是這樣的:

public class DBHelper extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "vocab.db"; 
    public static final String VOCAB_INPUT = "Input_Word"; 
    public static final String VOCAB_TRANSLATION = "Translation"; 
    public static final String DATE_TO_REMIND = "Date_To_Remind"; 
    public static final String TABLE_NAME = "Vocabulary"; 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String query = "CREATE TABLE " + TABLE_NAME + "(" 
       + VOCAB_INPUT + " TEXT " 
       + VOCAB_TRANSLATION + " TEXT " 
       + DATE_TO_REMIND + " TEXT" 
       + ");"; 
     db.execSQL(query); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

    public void addWord(Word word) { 
     ContentValues values = new ContentValues(); 
     values.put(VOCAB_INPUT, word.getWord()); 
     values.put(VOCAB_TRANSLATION, word.getTranslation()); 
     values.put(DATE_TO_REMIND, word.getDate()); 
     SQLiteDatabase db = getWritableDatabase(); 
     db.insert(TABLE_NAME, null, values); 
     db.close(); 
    } 

    public void deleteWord(Word word) { 
     SQLiteDatabase db = getWritableDatabase(); 
     db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + VOCAB_INPUT + "=\"" + word.getWord() + "\""); 
    } 

    public List<Word> getWordsToReview(String date) { 
     List<Word> returnWords = new ArrayList<>(); 
     SQLiteDatabase db = getReadableDatabase(); 
     Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + DATE_TO_REMIND + "='" + date + "'", null); 
     while (cursor.moveToNext()) { 
      String retrievedName = cursor.getString(cursor.getColumnIndex(VOCAB_INPUT)); 
      String retrievedTranslation = cursor.getString(cursor.getColumnIndex(VOCAB_TRANSLATION)); 
      String retrievedDate = cursor.getString(cursor.getColumnIndex(DATE_TO_REMIND)); 
      Word tempWord = new Word(retrievedName, retrievedTranslation, retrievedDate); 
      returnWords.add(tempWord); 
     } 
     cursor.close(); 
     return returnWords; 
    } 

    public boolean doesWordExist(Word word) { 
     SQLiteDatabase db = getReadableDatabase(); 
     Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + VOCAB_INPUT + "=\"" + word.getWord() + "\"", null); 
     boolean exists = false; 
     while (cursor.moveToNext()) { 
      String retrievedName = cursor.getString(cursor.getColumnIndex(VOCAB_INPUT)); 
      if (retrievedName.equalsIgnoreCase(word.getWord())) { 
       exists = true; 
       break; 
      } 
     } 
     cursor.close(); 
     return exists; 
    } 
} 
+1

不應該在'onCreate()'中的每個'TEXT'後面加一個逗號來分隔列嗎? – PPartisan

+0

另外請注意,您的日期格式不正確。它必須是'2016-03-05'。 –

+0

@BobMalooga你爲什麼這麼說?當我在另一個課程中格式化日期時,日期格式如下:2016/03/05 – NuffsaidM8

回答

3

您在創建表中缺少一些逗號,所以表不會按照您的預期創建;

String query = "CREATE TABLE " + TABLE_NAME + "(" 
       + VOCAB_INPUT + " TEXT, "   // <-- added comma 
       + VOCAB_TRANSLATION + " TEXT, "  // <-- added comma 
       + DATE_TO_REMIND + " TEXT" 
       + ");"; 
+0

這將是問題。我是一個doofus!我會盡快接受答案。 – NuffsaidM8