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

杀马特。学长 韩版系。学妹 提交于 2019-12-11 13:12:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/26158428/return-multiple-rows-as-an-array-from-a-websql-databse-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!