Skip to content
This repository was archived by the owner on Mar 25, 2020. It is now read-only.

UPDATE Operation

Ricky Tobing edited this page May 2, 2014 · 2 revisions

DbQuery is using SQLiteDatabase.update() in the back. However, the API wraps and add more stuffs to the interface.

The following code does the same thing - updating the price of computer to $110

recordUpdated = db.get("Products")
                      .update("Name = ?", "Computer")
                      .columns("Price").val(110)
                      .query();
...
recordUpdated = db.get("Products")
                      .update("Name = 'Computer'").val("Price", 110)
                      .query();
...
// assuming computerId is known
recordUpdated = db.get("Products")
                      .update(computerId).val("Price", 110)
                      .query();
...

Note that query() is called to get the recordUpdated. If you don't care about the returned value, you may not call query() at all.

#Multiple column updates The following code does the same thing - updating computer price to $110 and quantity to 10 in the Product table.

// updating price and quantity
recordUpdated = db.get("Products")
                      .update("Name = ?", "Computer")         // conditions
                      .val(new String[]{"Price", "Quantity"}, // columns
                           new Object[]{110, 10})             // values
                      .query();
...
recordUpdated = db.get("Products")
                      .update("Name = ?", "Computer")
                      .columns("Price", "Quantity")
                      .val(110, 10)
                      .query();
...

Note that query() is called to get the recordUpdated. If you don't care about the returned value, you may not call query() at all.

#Using ContentValues ContentValues is treated as first class citizen in DbQuery. Examine the following code:

ContentValues contentValues = new ContentValues();
contentValues.put("Price", 110);
contentValues.put("Quantity", 10);

...
recordUpdated = db.get("Products").update(contentValues, "Name = ?", "Computer").query();
assertTrue(recordUpdated > 0);
...
recrodUpdated = db.get("Products").update(contentValues, computerId).query();
assertTrue(recordUpdated > 0);
...
recordUpdated = db.get("Products").update(contentValues, "Name = 'Computer'").query();
assertTrue(recordUpdated > 0);

Note that query() is called to get the recordUpdated. If you don't care about the returned value, you may not call query() at all.

#Using IEntity With IEntity updating makes EASY

// assuming you have Product class which implements IEntity
public class Product implements IEntity {
   ...
}

...
// somewhere else
Product product = new Product();
product.setPrice(110);
product.setQuantity(10);
...
recordUpdated = db.get("Product").update(product).query();
assertTrue(product.getId() > 0);
assertTrue(recordUpdated > 0);

Note that query() is called to get the recordUpdated. If you don't care about the returned value, you may not call query() at all.

#Using IEntityList With IEntityList bulk-updating comes EASY too!

// assuming you have `ProductList` which implements `IEntityList<Product>`
public class ProductList extends ArrayList<Product> implements IEntityList<Product>{
   ...
}

...
// somewhere else
ProductList list = new ProductList();
// product Id, Name, Price, Quantity
list.add(new Product(1, "ProductA", 110, 10));
list.add(new Product(2, "ProductB", 110, 10));
list.add(new Product(3, "ProductB", 110, 10));
...
recordUpdated = db.get("Products").update(list).query();
assertTrue(recordUpdated == 3);
// records are now updated based on their Ids

Note that query() is called to get the recordUpdated. If you don't care about the returned value, you may not call query() at all.

Clone this wiki locally