Delete all items with a certain id from database
In my database I have 2 tables, one with lists and one with products. Each product has an id that means the number of the list it belongs. I want to create a menu for deleting all the products from a list. I have write this method, but it doesn't work. Any idea?
public boolean deleteproductall(long rowId) {
return mDb.delete(DATABASE_TABLE_2, KEY_LISTID + "=" + rowId, null) > 0;
}
table 2 :
private static final String DATABASE_TABLE_2 = "Products";
public static final String KEY_ROWID2 = "_id";
public static final String KEY_LISTID = "ListId";
public static final String KEY_ITEM = "Item";
public static final String KEY_QUANTITY = 开发者_如何学编程"Quantity";
public static final String KEY_UNITS = "Units"
and for viewing only the product from a list, I used :
public Cursor fetchListId(String listId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE_2, new String[] {
KEY_ROWID2, KEY_LISTID, KEY_ITEM, KEY_QUANTITY, KEY_UNITS },
KEY_LISTID + "=" + listId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Please try this. It solves your problem.
public void deleteProductAll(long rowId)
{
try
{
mDb.execSQL("DELETE FROM Products WHERE ListId = " + rowId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
KEY_LISTID is defined as String, where as rowId is a long. Change it to this
return mDb.delete(DATABASE_TABLE_2, KEY_LISTID + "=" + Long.valueOf(rowId), null) > 0;
精彩评论