使用javaScript从Web SQL数据库返回多行作为数组

时间:2014-10-02 09:55:05

标签: javascript arrays return-value web-sql

我想从WebSQL函数内的javaScript数据库中的特定表中返回多行作为数组。以下是我的代码。

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我只是设法使用Return a COUNT from a WebSQL query in a javaScript function

来做到这一点

根据链接,使用jQuery它就像这样,它可以工作!但我希望得到一个javaScript答案。

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
});