Telerik UI for Windows 8 HTML

This article explains the two ways to update data in a table using Data Storage API. The first one is through the update(storeName, data) method of the Database object. The other is to get ahold of the target table first through the use(storeName) method and than use the shorthand update(data) method. Both approaches are described in more detail in the following sections.

Update Records Using update(storeName, data) method

The two arguments accepted by the Database.update() method are:

  • storeName: The name of the table whose data should be updated.

  • data: The object containing the record with the updated values.

You can call the method multiple times and the data objects will be stored for update. To save the pending changes, call the sync() and close() methods of the Database object in this order.

The record id will be used to match the updated data object to a row in the table. Then, all differences from the passed data item will be applied to the one in the database. If there is no id match, no update will take place.

The example below demonstrates a data update in a table.

Update Records Example Copy imageCopy
db = Telerik.Data.Database.open("ProductsDB");
//id, name, category and price are variables containing string and numeric values, e.g. 21, "Pancakes", 3, 12
var product = { id: id, productName: name, categoryId: category, unitPrice: price };
db.update("Products", product);
db.sync().then(function (e) {
    db.close();
    queryDb(); //query the database for latest state of data after update
});

Update Records Using update(data) method

In this scenario, you first need to get a reference to the table. This is done via the use(storeName) method. Once you have the table reference, you can call the update(data) method one or more times.

The data argument that the update(data) method accepts must be an object containing the data values of the row that needs to be updated. Multiple calls to the method can be chained one after another. When all data is listed for update, call the sync() method at the end of the chain to trigger the actual update operations. The method returns a WinJS.Promise which you can use to close the database connection once the update operations have been finished (either successfully or with an error).

The record id will be used to match the updated data object to a row in the table. Then, all differences from the passed data object will be applied to the one in the database. If there is no id match, no update will take place for the current item.

Following is an example of update using the update(data) method.

Update Records Example Copy imageCopy
db = Telerik.Data.Database.open("ProductsDB");
var product = { id: id, productName: name, categoryId: category, unitPrice: price };
db.use("Products")
.update(product)
.sync().then(function (e) {
    db.close();
    queryDb(); //query the database for latest state of data after update
});

See Also