没有这样的列:aa(代码1):,而在编译时:从笔记中选择笔记,其中笔记= aa

时间:2018-06-25 07:13:45

标签: android sqlite

我试图防止重复 sqlite中的条目, 所以我用这段代码,但它给出了错误 没有这样的列:aa(代码1) 我的DatabseHelper类是:

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "db_notes";
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Note.CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
    onCreate(db);
}
public String insertNote(String note, String address) {
    SQLiteDatabase db  = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " + 
 Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = " + note ,null,null);
    if (resultSet.getCount() == 0)
    {
        values.put(Note.COLUMN_NOTE, note);
        values.put(Note.COLUMN_ADDRESS, address);
        db.insert(Note.TABLE_NAME, null, values);
    }

    else Toast.makeText(context,note+" already 
  exists",Toast.LENGTH_SHORT).show();
    return note;
}
public Note getNote(String id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Note.TABLE_NAME,
            new String[]{Note.COLUMN_ID, 
Note.COLUMN_NOTE,Note.COLUMN_ADDRESS, Note.COLUMN_TIMESTAMP},
            Note.COLUMN_ID + "=?",
            new String[]{String.valueOf(id)}, null, null, null, null);

    if (cursor != null)
        cursor.moveToFirst();
    Note note = new Note(
            cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
            cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
            cursor.getString(cursor.getColumnIndex(Note.COLUMN_ADDRESS)),
            cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
    cursor.close();
    return note;
}

public List<Note> getAllNotes() {
    List<Note> notes = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + Note.TABLE_NAME + " ORDER BY " +
            Note.COLUMN_TIMESTAMP + " DESC";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Note note = new Note();
            note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));

 note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
 note.setAddress(cursor.getString
 (cursor.getColumnIndex(Note.COLUMN_ADDRESS)));
 note.setTimestamp(cursor.getString
 (cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
            notes.add(note);
        } while (cursor.moveToNext());
    }
    db.close();
    return notes;
}
}

我收到此错误

  

android.database.sqlite.SQLiteException:没有这样的列:aa(代码1):,而在编译时:从notes中选择note note where where = aa

此行出现错误:

Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = " + note ,null,null);

2 个答案:

答案 0 :(得分:0)

需要在双引号中放入一个值。

将查询更改为此

Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " + 
        Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = \"" + note + "\"" ,null,null);

答案 1 :(得分:0)

一个SQLite exception,指示SQL解析或执行出错。

您应该添加 SINGLE QUOTE

在此处添加单引号-> "='" + note + "'"

 " SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME  + "  WHERE
  " + Note.COLUMN_NOTE + "='" + note + "'"

最后

 Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " +   Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + "='" + note + "'",null,null); 

然后 Clean-Rebuild-Run