3
我想在使用PhoneGap + jQuery Mobile構建Android應用程序時訪問HTML5數據庫。奇怪的是,下面的代碼可以得到不同的結果。它可以在Galaxy S 2.2(chris,lulu,chris)中獲得正確的結果,但是在Cliq TX(Android 2.1)中,應用程序被破壞(最後一個警報是createTable()),而在Emulator 2.3中,應用程序在第一次警報時崩潰。我拉出數據庫並發現它已正確創建。我認爲很難解釋這些問題,爲什麼這樣簡單的代碼會造成如此多的問題?劑量某人成功訪問數據庫?PhoneGap + jQuery Mobile訪問HTML5 Android 2.1中的Sqlite數據庫問題
預先感謝您。
function init() {
alert('init()');
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
if (!window.openDatabase) {
alert('Local Databases are not supported.');
} else {
db = window
.openDatabase("YCHW", "1.0", "YCHW", 200000);
}
dropTable();
createTable();
insertData();
selectData();
}
function dropTable(){
alert('dropTable()');
db.transaction(
function (transaction) {
transaction.executeSql("DROP TABLE measurements;", [], nullDataHandler, errorHandler);
}
);
console.log("Table 'measurements' has been dropped.");
// location.reload();
}
function createTable(){
alert('createTable()');
db.transaction(
function (transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS measurements(id INTEGER NOT NULL PRIMARY KEY, user TEXT NOT NULL, date TEXT NOT NULL, height INTEGER NOT NULL, weight INTEGER NOT NULL, bmi REAL NOT NULL, abnormal INTEGER NOT NULL);', [], nullDataHandler, errorHandler);
}
);
console.log("Table 'measurements' has been created.");
}
function insertData(){
alert('insertData()');
db.transaction(
function (transaction) {
var data = ['1','chris','2000-02-22 00:00:00.000','170','60', '20', '0'];
transaction.executeSql("INSERT INTO measurements(id, user, date, height, weight, bmi, abnormal) VALUES (?, ?, ?, ?, ?, ?, ?)", [data[0], data[1], data[2], data[3], data[4], data[5], data[6]]);
data = ['2','lulu','2000-02-22 00:00:00.000','170','60', '20.12', '0'];
transaction.executeSql("INSERT INTO measurements(id, user, date, height, weight, bmi, abnormal) VALUES (?, ?, ?, ?, ?, ?, ?)", [data[0], data[1], data[2], data[3], data[4], data[5], data[6]]);
data = ['3','chris','2222-02-22 00:00:00.000','170','60', '20.12', '1'];
transaction.executeSql("INSERT INTO measurements(id, user, date, height, weight, bmi, abnormal) VALUES (?, ?, ?, ?, ?, ?, ?)", [data[0], data[1], data[2], data[3], data[4], data[5], data[6]]);
}
);
console.log("Data has been inserted.");
}
function selectData(){
alert('selectData()');
db.transaction(
function (transaction) {
transaction.executeSql('SELECT * FROM measurements;', [], dataSelectHandler, errorHandler);
}
);
console.log("Data has been selected.");
}
function dataSelectHandler(transaction, results){
alert('dataSelectHandler()');
for (var i=0; i<results.rows.length; i++) {
var row = results.rows.item(i);
var measurement = new Object();
measurement.id = row['id'];
measurement.user = row['user'];
alert(measurement.user);
}
}
function nullDataHandler(){
alert('nullDataHandler()');
console.log("nullDataHandler()");
}
function errorHandler(transaction, error){
alert('errorHandler()');
if (error.code==1){
// DB Table already exists
alert('DB Table already exists');
} else {
// Error is a human-readable string.
console.log('Oops. Error was '+error.message+' (Code '+error.code+')');
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
}
return false;
}
嗨True North Creative,謝謝你的回覆,但我不太瞭解你。爲什麼你把errorHandler放在那裏?你能給出更詳細的答案嗎?謝謝!克里斯 – ThinkChris
哦,對不起 - 哈比。 errorHandler只是發生錯誤時的回調。你可以刪除它,或者添加到你的代碼中'function errorHandler(transaction,error){console.log('error occurred');}' – TNC