Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Highlighted
New Developer
Posts: 3
Registered: ‎11-11-2013
My Device: BB Z10
My Carrier: Idea
Accepted Solution

Cordova/Phonegap Storage API for WebSQL not working on BB10

I am using the Storage API for WebSQL in Phonegap with BB10 but its not working.

And not able to see any logs in web inspector. I am testing code on bb Z10 device having OS 10.2.

And having cordova 3.1.

Is there SDCard is required in device?? Right now there is no sdcard in my device.

Or is there any other configuration required. Please help me ASAP.

 

Please find my code below:

index.html

 

<!DOCTYPE html>

<html>

  <head>

    <title>Contact Example</title>

 

    <script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>

    <script type="text/javascript" charset="utf-8">

 

    // Wait for PhoneGap to load

    //

    document.addEventListener("deviceready", onDeviceReady, false);

 

    // Populate the database

    //

    function populateDB(tx) {

                                                              alert("populateDB");

        tx.executeSql('DROP TABLE IF EXISTS DEMO');

        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');

        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');

        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');

    }

 

    // Query the database

    //

    function queryDB(tx) {

                                                                alert("queryDB");

        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);

    }

 

    // Query the success callback

    //

    function querySuccess(tx, results) {

                                                             alert("querySuccess");

        var len = results.rows.length;

        console.log("DEMO table: " + len + " rows found.");

        for (var i=0; i<len; i++){

            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);

        }

    }

 

    // Transaction error callback

    //

    function errorCB(err) {

                                                                 alert("errorCB");

        console.log("Error processing SQL: "+err.code);

    }

 

    // Transaction success callback

    //

    function successCB() {

                                                               alert("successCB");

        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);

        db.transaction(queryDB, errorCB);

    }

 

    // PhoneGap is ready

    //

    function onDeviceReady() {

                                                           alert("onDeviceReady");

        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);

        db.transaction(populateDB, errorCB, successCB);

    }

 

    </script>

  </head>

  <body>

    <h1>Example</h1>

    <p>Database</p>

  </body>

</html>

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Cordova/Phonegap Storage API for WebSQL not working on BB10

You're using the document variable at the root of a <script> element, which will mean that <script> will try to execute as the page is being loaded. This means that document may not actually exist yet. A quick way to check this is to connect Web Inspector and launch the app, then in Web Inspector, hit ctrl+r to refresh the application and check the console for logs.

 

For a safe way to configure your application, can you wrap any calls to document inside of a window load event? Example:

 

window.addEventListener('load', function () {
    document.addEventListener('deviceready', onDeviceReady, false);
}, false);

This will ensure that document is available before trying to access it. The rest of the function declarations can remain as they are.

 

If this does not resolve the issue, please let us know and I'll put together a sample application here.

 

One other thought, the way I've seen DB requests in the past is by passing the size as follows: 5 * 1024 * 1024 as opposed to your 200000. I've seen some quirks if the size is too large (perhaps 200000 is too small?) or potentially the size works best as a multiple of 1024 (i.e. a true kb); these are more of a guess, but thought I'd mention it.

 

Finally, alerts can be...inconsistent. Especially if errors are occuring during load. I'd recommend replacing them with some console.log calls in conjunction with Web Inspector to monitor the activity.

 

Let me know if you have any questions.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.