У меня есть два метода для добавления и удаления данных из таблицы sqlite.Оба эти метода прекрасно работают отдельно.Что я хотел бы сделать, это реализовать оба метода на одной кнопке.При нажатии кнопки, если данные существуют в таблице, удалите данные, в противном случае добавьте данные, если их нет, и измените рисование в кнопке изображения.Я много чего перепробовал, но не могу понять, как это сделать.
Вот мой код
public boolean addBookmark(String id, String word, String definition) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_ID, id);
values.put(COL_WORD, word);
values.put(COL_DEFINITION, definition);
db.insert(TABLE_BOOKMARK, null, values);
return true;
}
public Integer deleteBookmark(String id){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_BOOKMARK, "id = ?",new String[]{id});
}
Я сделал что-то подобное, но это не работает, я получаю ошибкув logcat.
public boolean isBookmark(String id) {
SQLiteDatabase db = this.getWritableDatabase();
String Query = "Select * from " + TABLE_BOOKMARK + " where " + id + " = ?" ;
Cursor cursor = db.rawQuery(Query, null);
if(cursor.getCount() <= 0){
cursor.close();
return false;
}
cursor.close();
return true;
}
и в onclick
public void onClick(View v) {
boolean isBookmark = mDBHelper.isBookmark(id);
if (isBookmark){
mDBHelper.deleteBookmark(id);
btnBookmark.setImageResource(R.drawable.ic_bookmark_border);
} else {
mDBHelper.addBookmark(id,word,definition);
btnBookmark.setImageResource(R.drawable.ic_bookmark_fill);
}
вот моя ошибка logcat
2019-02-22 19:36:53.391 28210-28210/com.elytelabs.testnav E/SQLiteDatabase: Error inserting definition=Create a new folder by going to New > Directory . In the dialog box that opens name the directory fragment or any name of your choice.
Create a new Fragment file inside the created directory and name it dictionary or any word of your choice.
id=7 word=rvvg
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: bookmark.id (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.elytelabs.testnav.database.DatabaseHelper.addBookmark(DatabaseHelper.java:125)
at com.elytelabs.testnav.DetailActivity$1.onClick(DetailActivity.java:68)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)