我正在编写一个使用 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);
}