Android 在一个按钮上实现两种方法
我有两种方法可以在sqlite表中添加和删除数据。这两种方法分别工作得很好。我想做的是在一个按钮上实现这两种方法。 在按钮上,单击表中是否存在数据删除数据,否则添加数据(如果不存在)并更改图像按钮中的可绘制内容。我尝试了很多东西,但我不知道怎么做 这是我的密码Android 在一个按钮上实现两种方法,android,android-sqlite,buttonclick,android-imagebutton,Android,Android Sqlite,Buttonclick,Android Imagebutton,我有两种方法可以在sqlite表中添加和删除数据。这两种方法分别工作得很好。我想做的是在一个按钮上实现这两种方法。 在按钮上,单击表中是否存在数据删除数据,否则添加数据(如果不存在)并更改图像按钮中的可绘制内容。我尝试了很多东西,但我不知道怎么做 这是我的密码 public boolean addBookmark(String id, String word, String definition) { SQLiteDatabase db = this.getWritableDataba
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;
}
这是我的日志错误
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)
首先需要检查数据是否存在,然后可以添加或删除数据 例如,使用这样的方法
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;
}
我认为
isBookmark
中的以下代码行不正确:
String Query = "Select * from " + TABLE_BOOKMARK + " where " + id + " = ?" ;
我相信这将创建一个select语句,如下所示:
"Select * from TABLE where 5 = ?"
String Query = "Select * from " + TABLE_BOOKMARK + " where id = " + id;
*当然,我不知道你的TABLE_BOOKMARK const值实际上是什么,所以我把表名作为TABLE放进去。我任意使用了id值5
声明的格式应如下所示:
"Select * from TABLE where 5 = ?"
String Query = "Select * from " + TABLE_BOOKMARK + " where id = " + id;
这似乎也有道理,因为这意味着isBookmark永远不会删除您希望它删除的记录值(因为它永远不会成功找到与查询匹配的记录)。这意味着插入总是失败,因为记录实际上仍然存在。onclick方法中的简单逻辑语句和问题中定义的逻辑应该可以解决问题。onclick方法中的代码是什么?我需要添加id、word、定义还是仅添加id,word将检查行是否存在,我认为您只需要检查
id
,因为这应该是唯一的。谢谢。您的解决方案工作得很好,但我无法在单击if-else语句时为其设置不同的可绘制内容,因为可绘制内容会发生更改。但情况并非如此