Telerik UI for Windows 8 HTML

This article explains the two ways to populate a table using Data Storage API. The first one is through the insert(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 insert(data) method. Both approaches are described in more detail in the following sections.

Insert Records Using insert(storeName, data) method

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

  • storeName: The name of the table into which data should be inserted. If the table does not exist, it will be created.

  • data: The object containing the record to be inserted.

You can call the method multiple times and the data objects will be stored for insert. To save the pending changes, call the sync() method of the Database object.

The example below demonstrates an insert into a database table.

Insert 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.insert(product)
db.sync().then(function (e) {
    db.close();
    queryDb(); //query the database for latest state of data after insert
});
Note

The inserted items must have a field called id which carries unique values for each record. This field will be used to recognize records when you update and delete them.

Insert Records Using insert(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 insert(data) method one or more times.

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

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

Insert 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.use("Products")
.insert(product)
.sync().then(function (e) {
    db.close();
    queryDb(); //query the database for latest state of data after insert
});
Note

The inserted items must have a field called id which carries unique values for each record. This field will be used to recognize records when you update and delete them.

See Also