2014-03-26 68 views
0

我下面this example哪些錯誤與此代碼

我在代碼遍地看了一遍,似乎幾乎相同的例子然而getOrdersSuccess不會被調用。日誌顯示「setupTable」和「getOrders」都被調用。 getOrdersSuccess中沒有任何記錄。

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>"); 
    } 
+0

@JoachimIsaksson我更新了問題以包含錯誤函數。日誌不會拋出錯誤。當我在我的聯繫上運行應用程序時,我沒有看到任何錯誤。發佈的代碼是手機差距應用程序的一部分。我還沒有真正找到phonegap調試。我想我應該仔細研究一下。 – JoeKincognito

回答

1

定義var db;之外的任何功能,然後修改onDeviceReady這樣的:

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

的問題是,如果定義var db裏面的onDeviceReady它的作用範圍是該功能,並在功能無法訪問getOrders

+0

非常感謝!只要我讀到這個,我就想衝着自己的臉,同時擁抱你。 – JoeKincognito