在一个按钮上实现两种方法

时间:2019-02-21 18:26:46

标签: android android-sqlite buttonclick android-imagebutton

我有两种方法可以从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)

2 个答案:

答案 0 :(得分:0)

首先需要检查数据是否存在,然后可以添加或删除数据。

例如,使用这种方法。

public static boolean CheckIsDataAlreadyInDBorNot(String TableName,
        String dbfield, String fieldValue) {
    SQLiteDatabase sqldb = EGLifeStyleApplication.sqLiteDatabase;
    String Query = "Select * from " + TableName + " where " + dbfield + " = " + fieldValue;
    Cursor cursor = sqldb.rawQuery(Query, null);
        if(cursor.getCount() <= 0){
            cursor.close();
            return false;
        }
    cursor.close();
    return true;
}

取自https://stackoverflow.com/a/20416004/3106174

然后,您可以简单地完成操作。

if( CheckIsDataAlreadyInDBorNot( ... ) ) {
    deleteBookmark( ... );
} else {
    addBookmark( ... );
}

答案 1 :(得分:0)

我认为isBookmark中的以下代码行是错误的:

String Query = "Select * from " + TABLE_BOOKMARK + " where " + id + " = ?" ;

我相信这将创建一个如下所示的select语句:

"Select * from TABLE where 5 = ?"

*当然,我不知道您的TABLE_BOOKMARK常量值实际上是什么,因此我将表名放在TABLE中。而且,我随意使用了ID值5。

该语句应类似于以下内容:

String Query = "Select * from " + TABLE_BOOKMARK + " where id = " + id;

这似乎也很有意义,因为这意味着isBookmark永远不会删除您希望其删除的记录值(因为它永远不会成功找到与查询匹配的记录)。这意味着插入总是失败,因为记录实际上仍然存在。