开发者

Unable to retrieve data from web sql database in phonegap app

I am working on a phonegap app on iOS that needs to store some data. I am using the storage API given in phonegap docs and I believe the data is being inse开发者_如何学Crted. But when I am trying to retrieve the data using SELECT statement, I am not getting any output in the alert boxes.

the "loading2" alert is being shown but after that i dont get any output.

My code is as below (picked up from phonegap wiki):

// load the currently selected icons
    function loadCelebs(mydb)
    {
        try 
        {
            alert("loading2");
            mydb.transaction(
                function(transaction) 
                {
                    transaction.executeSql('SELECT * FROM celebs ORDER BY name', [], celebsDataHandler(transaction,results));
                });
        } 
        catch(e) 
        {
            alert(e.message);
        }
    } 


    // callback function to retrieve the data from the prefs table

    function celebsDataHandler(tx, results) 
    {

        // Handle the results 
            alert(results);

    }


Try changing it to:

 mydb.transaction(
            function(transaction) 
            {
                transaction.executeSql('SELECT * FROM celebs ORDER BY name', [], celebsDataHandler);
            });

You don't want the (tx, results) part in the transaction() call. You are only passing a reference to the callback handler so that it can run it when it is done. By adding the (tx, results) you are executing and passing in the result.

Also, consider passing in an error handler. It can make debugging easier if you know what the errors are.

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}


mydb.transaction(
    function(transaction) 
    {
        transaction.executeSql('SELECT * FROM celebs ORDER BY name', errorCB, celebsDataHandler);
    }
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜