Gianluca Ghettini
Gianluca Ghettini

Reputation: 11628

Cordova SQLite - get results from a sequence of SELECT queries

I have a Cordova mobile app using a SQLite database. I'm using the cordova-sqlite-storage plugin. I need to perform two selects one after another and get the results. Is it possible to avoid the nesting? And if so, how? Couldn't find any example on how to do it.

P.S. I need to exit the function with both results from both queries

ctx.transaction(function(tx) {
    tx.executeSql("SELECT * FROM cards WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
        // get the result here from first select
        tx.executeSql("SELECT * FROM events WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
            // get the result from second select and return
            // return with results from both queries
        }, function(tx, error) {
            // fail
        });
    }, function(tx, error) {
        // fail
    });
});

Upvotes: 1

Views: 1292

Answers (1)

Connor
Connor

Reputation: 64644

Since you don't need the result of the first select, I don't think you need to nest. Something like this should work:

var result1, result2; 
ctx.transaction(function(tx) {
    tx.executeSql("SELECT * FROM cards WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
        // get the result here from first select
        result1 = ...;
    }, function(tx, error) {
        // fail
    });
    tx.executeSql("SELECT * FROM events WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
        // get the result from second select and return
        result2 = ...;
    }, function(tx, error) {
        // fail
    });
}, null, function() {
    //do something with results;
});

The plugin should still queue and execute them in order.

Upvotes: 1

Related Questions