я пытаюсь предотвратить дублирование
записи в sqlite,
так что я использую этот код, но он дает ошибку
нет такой колонки: аа (код 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): при компиляции: SELECT note From notes, WHERE note = aa
Я получаю сообщение об ошибке в этой строке:
Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " + Note.TABLE_NAME + " WHERE " + Note.COLUMN_NOTE + " = " + note ,null,null);