-
Notifications
You must be signed in to change notification settings - Fork 4
DELETE Operation
DbQuery is using SQLiteDatabase.delete() in the back. However, the API wraps and add more stuffs to the interface.
The following code does the same thing - removing computer from Products table.
numDeleted = db.get("Products")
.delete(productId)
.query();
...
numDeleted = db.get("Products")
.delete("Name = ?", "Computer")
.query();
...Note that query() is called to get the numDeleted. If you don't care about the returned value, you may not call query() at all.
#Bulk-deletion
Using Id. The following code does the same thing - deleting multiple products using their Id
long[] idsToRemove = ...
numDeleted = db.get("Products")
.delete(idsToRemove)
.query();
...
List<Long> idListToRemove = ...
numDeleted = db.get("Products")
.delete(idListToRemove)
.query();
...
// using varargs...
numDeleted = db.get("Products")
.delete(1,2,3,5,6,7,...)
.query();Note that query() is called to get the numDeleted. If you don't care about the returned value, you may not call query() at all.
#Bulk-Deletion using a condition The following code shows bulk-deletion using some condition - we're deleting any product whose name starts with "Key" (will include: "Keychain", "Keyboard", "Key"....)
...
numDeleted = db.get("Products")
.delete("Name LIKE ?", "Key%")
.query();
...Note that query() is called to get the numDeleted. If you don't care about the returned value, you may not call query() at all.
#Using IEntity
Calling delete(IEntity) is equivalent to calling delete(IEntity.getId())
// assuming you have Product class which implements IEntity
public class Product implements IEntity {
...
}
...
// somewhere else in the code
Product product = new Product();
product.setId(someId);
...
numDeleted = db.get("Product")
.delete(product)
.query();
assertTrue(numDeleted == 1); // one product gets deletedNote that query() is called to get the numDeleted. If you don't care about the returned value, you may not call query() at all.
#Using IEntityList
Calling delete(IEntityList<IEntity>) is equivalent to calling delete(int...) which is bulk-deletion
// assuming you have `ProductList` which implements `IEntityList<Product>`
public class ProductList extends ArrayList<Product> implements IEntityList<Product>{
...
}
...
// somewhere in the code
ProductList productList = new ProductList();
productList.add(productA);
productList.add(productB);
...
numDeleted = db.get("Products")
.delete(productList)
.query();
assertTrue(numDeleted == productList.size());Note that query() is called to get the numDeleted. If you don't care about the returned value, you may not call query() at all.