SQLite with Cordova: Unable to initialize database on other pages

725 views Asked by At

I'm playing around SQLite in Cordova as part of an upskilling process for work and I'm hitting a brick wall. The various articles I've read around initializing the SQLite plugin from Chris Brody is to always call it in after device ready, but all examples are around the index page. What if I need to populate data on the products.html page, without also calling all other initialization calls to the database?

What I mean is, given the following JS file, called core.js:

var db,
    app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function () {        
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function (id) {
        app.initdb();
        console.log('Received Event: ' + id);
    },
    initdb: function () {

        try {

            db = window.sqlitePlugin.openDatabase({ name: 'meatblock.db' });

            if (!db) {
                console.error('Database unable to initialize, it either does not exist or is null');
                return false;
            }
            else {
                return true;
            }

        }
        catch (err) {
            console.error('Database initialization error: ' + err);
        }

    }
};

In the receivedEvent, which bubbles up, I call my initdb() function that calls the plugin and opens up the database.

The process works like a charm, in this method I can write my SQL SELECT statement to retrieve data and display it on the page without error.

As soon as I mode the TX script outside of this, it does not work. I even call the initdb() function before it, and still, I get an error saying that it cannot open database on undefined.

in core.js, at the top, I define db globally, as some have suggested in various other blogs, but the following code, out side of the receivedEvent just does not work:

    jQuery(document).ready(function ($) {
        app.initdb();
        db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM table_1', [], function (tx, results) {

                var _data = results;

                for (var i = 0; i < results.rows.length; i++) {
                    var row = results.rows.item(i);
                    $li = $('<li></li>').text(row);
                    $('.table-output').append($li);
                }

            }, function (e) {
                alert('an error occurred trying to retrieve database from table_1');
            });

        }, function (e) {
            alert('an error occurd');
        }, function () {
            alert('all done');
        });
    });

after calling app.initdb() just before I handle a TX, my assumption is that it would open the database again, as at this point, right? Even if I don't use jQuery's ready statement, it just does not work, without jQuery:

    app.initdb();
    db.transaction(function (tx) {
        tx.executeSql('SELECT * FROM table_1', [], function (tx, results) {

            var _data = results;

            for (var i = 0; i < results.rows.length; i++) {
                var row = results.rows.item(i);
                $li = jQuery('<li></li>').text(row);
                jQuery('.table-output').append($li);
            }

        }, function (e) {
            alert('an error occurred trying to retrieve database from table_1');
        });

    }, function (e) {
        alert('an error occurd');
    }, function () {
        alert('all done');
    });

I'm sure there is something that I'm not getting about this. Is it impossible to open the database and retrieve data outside of the device ready statement?

0

There are 0 answers