I am trying to access an HTML5 database while creating an Android application using PhoneGap + jQuery Mobile. It is strange that the following code can get different results. It can get the correct result in Galaxy S 2.2 (chris, lulu, chris), but in Cliq TX (Android 2.1) the application gets crushed (the last warning is createTable ()), and in emulator 2.3 the application crashed on the first notification, I pulled out the database data and found that it is correctly created. I think it’s hard to explain these problems, why do such simple codes cause so many problems? Does someone have success in accessing the database?
Thanks in advance.
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.");
}
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){
alert('DB Table already exists');
} else {
console.log('Oops. Error was '+error.message+' (Code '+error.code+')');
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
}
return false;
}