JoeKincognito
JoeKincognito

Reputation: 371

Whats wrong with this code

I am following this example

I have looked at the code over and over again and it seems nearly identical to the example however getOrdersSuccess never gets called. The log shows "setupTable" and "getOrders" both get called. Nothing from getOrdersSuccess ever gets logged.

function onDeviceReady() {
    if( window.isphone ) {
    var db = window.openDatabase("Database", "1.0", "The Database", 200000);
    db.transaction(setupTable, errorCB, getOrders);
    }
}
function setupTable(tx){
    $('#log').append("<p>setupTable</p>");
        tx.executeSql('create table if not exists orders (Id INTEGER PRIMARY KEY, name, isSubmitted, date)');
}
function getOrders() {
        $('#log').append("<p>getOrders</p>");
        db.transaction(function(tx){
            tx.executeSql('SELECT Id, name FROM orders', [], getOrdersSuccess, errorCB);
        }, errorCB);
    }
    // Query the success callback
    //
function getOrdersSuccess(tx, results) {
    $('#log').append("<p>getOrdersSuccess</p>");
        var len = results.rows.length;
        $('#log').append("<p>Orders table: " + len + " rows found.</p>");
        $('#current').html('');
        for (var i=0; i<len; i++){
            $('#log').append("<p>Row = " + i + " ID = " + results.rows.item(i).Id + " Name =  " + results.rows.item(i).name + "</p>");
            $('#current').append('<p>'+results.rows.item(i).Id+'---'+results.rows.item(i).name+'</p>');
        }
    }
function errorCB(err) {
        $('#log').append("<p>Error processing SQL: "+err.code+"</p>");
    }

Upvotes: 0

Views: 58

Answers (1)

Dawson Loudon
Dawson Loudon

Reputation: 6029

Define var db; outside of any function and then revise onDeviceReady like this:

var db;
function onDeviceReady() {
    if( window.isphone ) {
        db = window.openDatabase("Database", "1.0", "The Database", 200000);
        db.transaction(setupTable, errorCB, getOrders);
    }
}

The issue is, if you define var db inside of onDeviceReady it is scoped to that function and not accessible in the function getOrders

Upvotes: 1

Related Questions