简体   繁体   中英

Return multiple rows as an array from a WebSQL databse using javaScript

I want to return multiple rows as an array from a particular table in a database in WebSQL inside a javaScript function. Below is my code.

    function getCustomerAccountDetails(){
        var dataset;
        db.transaction(function(tx) {
            tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
                dataset = results.rows;
            });
        });
        return dataset
    }

Any suggestions?

I just managed to do that using Return a COUNT from a WebSQL query in a javaScript function

According to the link, using jQuery it goes like this and it works! But I would like to have a javaScript answer.

function getCustomerAccountDetails(){
    var defer = $.Deferred();
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
            defer.resolve(results.rows);
        });
    });

    return defer.promise();
}

function exampleThatUsesUserArray(data) {
    //do something that uses count here
    return data;
}

var dataArray = getCustomerAccountDetails();

$.when(dataArray).done(function(data) {
            //now use count, could be you call another function that needs to use count,

            //getCustomerAccountDetails();          
            alert(exampleThatUsesUserArray(data.length) + " Row Count");

            for(var i = 0; i < data.length; i++){
                alert((i+1) + "-" + data.item(i)['id'] + "-" + data.item(i)['firstname'] + "-" 
                        + data.item(i)['lastname'] + "-" + data.item(i)['phonenumber']);
            }

           //or assign it to another variable, or trigger an event that someone else in you app is    listening for
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM