For some reason my database is failing to execute a .delete method.
I have this check:
if(db.delete(clicked)){
Log.i("MainAct.class","Deleted row: " + clicked + " successfully");
} else {
Log.i("MainAct.class","FAILURE Deleting row: " + clicked + " UNsuccessfully");
}
It keeps returning “FAILURE” so I figure that means it is not deleting
public boolean delete(String row){
open();
Log.i("SmsDatabase.java", "DELETING: " + TABLE_NAME +
" id =? " + "id = '" + row + "'");
return this.db.delete(TABLE_NAME, "id = '" + row + "'", null) > 0;
}
It is called fine (btw, the open() method has a if(!db.isOpen()) so it doesn’t just always open my database) but it returns false for some reason.
I tried this:
return this.db.delete(TABLE_NAME, “id =?”, new String[]{row}) > 0;
But in the Log it returns weird characters along the line of: “[Lstring@406865” (dont remember exactly) for String clicked. And obviously that is not what I want.
Why is it doing this?
I’m going to make some assumptions in my answer. (1) The list of whatever you are wanting the user to be able to select for removal is filled with data that was obtained from the database that you are removing it from, (2) You have done the setup in onCreate or something like that to setup the listView’s click listener, or (3) set up the listview to be registered for a Context menu. I suspect that you are just not getting the correct ID for the item to be deleted.
In my setup I have a listview that has registered for a context menu, I use the context menu to let them select to edit or remove the item from the list so in my onCreate I call
registerForContextMenu(this.listView);In my context menu I have:
Then
Then
In my database helper’s code I call the following:
Another helpful function to get a particular item. Note if remember to replace Item/YOUR_ITEM with your object and update the query with your table name (YOUR_TABLE) and the BaseColumns._ID as needed.
I’m not a very experienced DB guy, so I relied heavily on a lot of the examples out that that had coded a database helper class (extends SQLiteOpenHelper) that facilitated getting and putting data into the SQLiteDataBase. Hope this helps!