我尝试在我的设备上运行此示例代码,它只能返回“设备就绪!”。它似乎没有运行db.transaction,因为它没有提醒其余的msg。但是当我尝试在Web浏览器上运行它时,它能够在控制台日志中显示所有警报和数据库结果。请帮忙。我真的不知道出了什么问题。提前谢谢!
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady(), false);
// Populate the database
//
function populateDB(tx) {
alert("Populate DB!");
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Query the database
//
function queryDB(tx) {
alert("Database Query!");
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
// Query the success callback
//
function querySuccess(tx, results) {
alert("Success!");
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
}
}
// Transaction error callback
//
function errorCB(err) {
alert("Error Occured!");
console.log("Error processing SQL: "+err.code);
}
// Transaction success callback
//
function successCB() {
alert("Open Database!");
db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);
}
// Cordova is ready
//
function onDeviceReady() {
alert("Device Ready!");
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}