0

我正在编写一个使用 Sqlite 在本地存储数据的移动应用程序。在过去的 4 天里,我一直试图弄清楚为什么当 Jquery 和 phonegap 完全加载时没有创建数据库。sqlite 中的 create 语句不起作用,回调函数也不起作用。deviceready 不起作用,但如果检查 sqlite 支持它会触发。示例代码是别人的代码,但同样的事情发生了。有人可以帮帮我吗?

var jqmReady = $.Deferred(),
pgReady = $.Deferred();

// jqm page is ready
$(document).bind("pageinit", jqmReady.resolve);

// phonegap ready
document.addEventListener("deviceready", pgReady.resolve, false);

// all ready, throw a custom 'onDeviceready' event
$.when(jqmReady, pgReady).then(function(){
  $(document).trigger("onDeviceready"); 
});


function onDeviceReady(){
        db.transaction(populateDB, errorCB, successCB);
    }

    //create table and insert some record
    function populateDB(tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)');
        tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")');
        tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")');
    }

    //function will be called when an error occurred
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }
    //function will be called when process succeed
    function successCB() {
        alert("success!");
        db.transaction(queryDB,errorCB);
    }
4

1 回答 1

0

我没有在模拟器或物理设备上运行它,但通过查看代码,我可以立即看到一个问题。试试这个,看看它是否有帮助:

改变

$.when(jqmReady, pgReady).then(function(){
     $(document).trigger("onDeviceready"); 
});

$.when(jqmReady, pgReady).then(function(){
     onDeviceReady();
});

我建议更改的原因是因为$(document).trigger("onDeviceready")触发了“onDeviceready”事件。您没有设置侦听器来捕获该事件,我假设您想要它做的是调用“onDeviceReady()”函数。

于 2013-02-10T01:28:08.787 回答