简体   繁体   中英

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

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