10-04-2012 12:33 AM
I'm tryna create a database and store some data in it. When I test my code on my blackberry 9900, it alerts me that the database was created successfully and that the table I wanna create too was created successfully. The problem is, whenever I want to do an insert or a select, i get an error that the database table does not exist. Please is there a problem with HTML5 database and BB Web Works. Here's a sneak into my code.
function createDB(){
if(window.openDatabase){
globalVar.db = window.openDatabase("TestDB8","1.0","Notification storage",5*1024*1024, onDBCreate);
}else{
alert("This device does not have HTML5 database support");
}
}
function onDBCreate(database){
database.transaction(function(tx){
tx.executeSql('CREATE TABLE notification(`id` INTEGER PRIMARY KEY AUTOINCREMENT , `subject` varchar(255), `message` varchar(4000), `time` datetime, `status` tinyint(1) DEFAULT 0)',[],
function(tx, res){
alert("Database Table notification was successfully created");
},
function(tx, err){
alert("Error creating table: "+err.message);
}
);
}
);
}
globalVar.db.transaction(function(tx){
tx.executeSql('INSERT INTO notification(`subject`,`message`,`time`,`user_id`,
function(tx,res){
alert('Notification was added successfully');
},
function(tx,err){
alert('Couldnt add notification: '+err.message);
}
);
}
);
globalVar.db.readTransaction(function(t){
t.executeSql('SELECT count(*) AS c FROM notification', [], function(t, r){
var len = r.rows[0].c;
alert("Total notifications: "+len);
},
function(t, e){
alert("Can't read records: "+e.message);
}
);
});
Please can someone tell me what's wrong. I read through the documentation closely and followed the instructions there.
10-04-2012 07:35 AM - edited 10-04-2012 08:11 AM
From the api
we see:
void transaction (callback: function(transaction : SQLTransaction), [errorCallback: function(error : SQLError)], [successCallback: function()])
Your success and error callbacks are the wrong way round . . .
BTW: A gotcha that cought me out when testing this that I didnt see in th docs is that the blackberry must be unplugged from usb after deployment for testing (at least on OS 5.0). Access to the SD card is required and there is no access when the the phone is connected via USB.
Edit: I suspect your create table is erroneously reporting success when it is actually failing because the table is already created and the inserts are erroneously reporting a fail when they are actually succeeding . . .