SQLite数据库列不存在

时间:2016-03-05 18:21:30

标签: android sqlite

我查看了一些关于列不存在错误的问题,并且大多数问题都是间距问题或与架构有关的问题,可以通过卸载并重新安装应用程序来解决。我已经校对了我的声明,并尝试从我的手机卸载应用程序并从android studio重新下载。似乎都没有解决我的问题。

这一行是抛出错误:

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

它的目的是将表中的每个元素(及其列对应部分)放在具有我想要的日期(这是我拥有的方法中的参数(getWordsToReview)。

我不断得到的错误是:

  

引起:android.database.sqlite.SQLiteException:没有这样的列:Date_To_Remind(代码1):,同时编译: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 个答案:

答案 0 :(得分:3)

您在创建表格中遗漏了一些逗号,因此表格不会像您期望的那样被创建;

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