Я пытаюсь добавить значение в виде списка из базы данных sqlite
, когда я добавляю значение в базу данных, это выдает ошибку
android.database.CursorIndexOutOfBoundsException:
Индекс 0 запрошен, с размером 0 я также ставлю условие для проверки того жезначение Вот мой DatabaseHelper
класс.
Как устранить эту ошибку?
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);
this.context = context;
}
@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, Context context) {
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.moveToFirst())
{
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.moveToFirst())
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());
}
cursor.close();
db.close();
return notes;
}
}
Ошибка в logcat:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
at info.androidhive.sqlite.database.DatabaseHelper.getNote(DatabaseHelper.java:63)