Telerik UI for Windows 8 HTML

You can execute read operations in Data Storage by either writing SQL queries and passing them to the query() method or using the Data Storage API. The latter option is easier to use and maintain but as of current does not cover all read scenarios (for example, join operations). This article will explain the two ways to perform read operations when using the Data Storage component.

Using SQL Statements

You can execute SQL queries that you have written against the database. The syntax of a query must conform to the SQLite synax. See reference here: SQL As Understood By SQLite.

Once you build an expression, use the query() method to execute it. The method returns a WinJS.Promise object. When the promise is fulfilled, an array with the resulting data records is passed to the onComplete function.

The example below shows how to get a filtered data set using the query() method.

Using the query() Method Copy imageCopy
var sql = "SELECT name, bo FROM Sales WHERE name LIKE 'R%' AND bonus > 100";
db = Telerik.Data.Database.open("SalesDB");
db.query(sql).then(querySuccess);

In this and the rest of the examples in this article, querySuccess() is a function that processes the returned data and closes the database connection:

Success Function Copy imageCopy
function querySuccess(result) {
    showResults(result);
    db.close();
}

The query() method can be used for simple select scenarios or when you already have your SQL statements prepared. In most scenarios, it will be easier and faster to query your data using the Data Storage API described in the next section.

Using Data Storage Fluent API

To get data easily and without long SQL statements, you can use the Data Storage fluent API. You can join tables, apply multiple group, filter and sort expressions without the need of maintaining long SQL strings. Furthermore, using IntelliSense, the fluent API guides you through the exact order of execution of functions. This is done by dynamically updating the list of available methods to call upon typing.

To be able to query data using the Data Storage method chaining, start with the get(tableName) method that receives the name of the table, that you want to query, as an argument. Then, you can optionally apply various data functions using the methods from the fluent API. Finally, call the execute() method which will trigger the entire chain of methods and perform a read operation.

To control the selected data items, you can also use the distinct() and fields(fieldsObj) methods. distinct() removes duplicate records from the query result based on the selected fields. fields(fieldsObj) receives an object argument which lists the fields that should be fetched.

The order in which you can call operations is listed below. Any method can be skipped if not needed. You will be prompted by IntelliSense about the possible methods that you could call at each step. If you do not follow the correct order, you will receive the following error: JavaScript runtime error: Object doesn't support property or method '...' .

  • join

    Can be called multiple times.

  • group

    Can be called multiple times.

  • distinct

    Can be called one time.

  • filter

    Can be called one time. For applying additional filter expressions, use and and or.

  • sort

    Can be called multiple times.

  • skip/take

    Can be called one time.

  • fields

    Can be called one time.

Important

Keep in mind that the actual operations are not performed until the entire method chain is triggered by the execute() method.

See Also