Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
Posts: 45
Registered: ‎12-27-2011
My Device: Developer
My Carrier: None

websql/sqlite weirdness transaction fails silently

[ Edited ]

i have a method that inserts a row in a table:

 

function insertListItem(list_id, itemname, itemamount, itemprice, ischecked) {
db.transaction(function(tx){
try {
tx.executeSql("INSERT INTO list_contents (list_id, name, amount, price, checked) VALUES (?, ?, ?, ?, ?);", [list_id, itemname, itemamount, itemprice, ischecked], function() {alert('win');}, function() {alert('fail');});
}
catch(ex) {
alert("ex:" + ex.message);
}
}, sqlFail);
}
 
this method is being called from 2 pages
one that creates a new list and adds items for it, and one that edits/updates a list and its items
but the method only works when called from the page that creates a new list

from the other page the db.transaction just fails silently
i tried to put an alert inside the db.transaction but i never get triggered
im pretty baffled at the moment.. i checked the parameter values in the chrome debugger and they are fine
 
does anyone have any idea what is happening here?
Contributor
Posts: 41
Registered: ‎02-14-2012
My Device: Curve 8900 / Playbook
My Carrier: Rogers

Re: websql/sqlite weirdness transaction fails silently

Did you confirm that your function is actually getting called?

Did you make sure you declared this function on both pages? Presumably you included the .js file on both pages or whatever file you've declared this in?

Developer
Posts: 71
Registered: ‎02-06-2013
My Device: 9900
My Carrier: Rogers

Re: websql/sqlite weirdness transaction fails silently

[ Edited ]

Are you giving the transaction time to complete?

If you're performing some other action after your "insertListItem" function, make sure you delay it to give the DB enough time for the transaction.

 

ie.

 

function main(){
  insertListItem(v,w,x,y,z);
  setTimeout(function(){location.reload()},500);
}

 

Contributor
Posts: 45
Registered: ‎12-27-2011
My Device: Developer
My Carrier: None

Re: websql/sqlite weirdness transaction fails silently

andrew: yeah its included and im sure the method actually starts since i had i add alert('blabla'); to try and debug
when i added one inside the db.transaction function scope it never got triggered though

Contributor
Posts: 45
Registered: ‎12-27-2011
My Device: Developer
My Carrier: None

Re: websql/sqlite weirdness transaction fails silently

mike: hmmm thats an idea it never occurred to me that this would be an issue as i am used to languages where code doesnt "fall out of scope" like that... i will check it out! thanks Smiley Happy

New Contributor
Posts: 7
Registered: ‎02-27-2013
My Device: developer
My Carrier: ATT

Re: websql/sqlite weirdness transaction fails silently

first thing i notice is the semi-colon in the sql statement.

 

Here is how I would do it...

 

 

function insertListItem(list_id, itemname, itemamount, itemprice, ischecked) {
     var sql = "INSERT INTO list_contents (list_id, name, amount, price, checked) VALUES (?, ?, ?, ?, ?)";
     db.transaction(function(tx){
          tx.executeSql(sql, [list_id, itemname, itemamount, itemprice, ischecked], querySuccess, errorCB);
     });
}

 

function querySuccess(tx, results){
     console.log('returned rows = ' + results.rows.length);
}

 

function errorCB(err){
     console.log('error processing sql: ' + err.code);
}

Contributor
Posts: 45
Registered: ‎12-27-2011
My Device: Developer
My Carrier: None

Re: websql/sqlite weirdness transaction fails silently

im not sure how moving the query into a variable changes anything?

the semicolon is the standard SQL language query terminator, its not always necessary to put depending on the db/connector but is good practice as far as i know