繁体   English   中英

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

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

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

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

有什么建议么?

我只是使用javaScript函数中的从WebSQL查询返回COUNT来做到这一点

根据链接,使用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
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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