user1924004
user1924004

Reputation: 1

WebSQL return object

Can someone show me a web SQL select query that returns the results as an object rather than alerting or logging to the console.

I want to centralize my select queries rather than repeating the select / execute and process results code in the specific functions.

Upvotes: 0

Views: 962

Answers (1)

Rob Von Nesselrode
Rob Von Nesselrode

Reputation: 801

If we presume that the question relates to the API required under Web SQL to execute the query and obtain a result set you can proceed as shown below. I've not given much detail as the question is a tad vague and shows little evidence of homework...

  1. Initiate a readTransaction (you did say 'query' so I'll presume "select") on an open database:

    db.readTransaction(onStartTransaction); // less locking overhead with a readTransaction

The onStartTransaction function looks like this:

function onStartTransaction(tx) {
        tx.executeSql(sql, params, onExecuteSqlOk, onExecuteSqlFail);
    }

Just pass your sql and a [] for params if you don't need any.

Your results will be returned to your 'onExecuteSqlOk' function:

function onExecuteSqlOk(tx, result) {
        //
        // tx: an SqlTransaction object
        // results: an SqlResultSet object
        //

The goodies are in the SqlResultSet object.

it has a structure with a 'rows' property. Each row contains the fields specified in the sql select statement.

var len = results.rows.length; // how many rows did we get
var firstRow = results.rows.item(0);

Thus you get an "object" back as the results from a Web Sql query. Note that the api shown is asynchronous so you will probably want to use a further callback function....

Upvotes: 1

Related Questions