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: 41
Registered: ‎02-14-2012
My Device: Curve 8900 / Playbook
My Carrier: Rogers

WebSQL not working on Ripple Simulator

I'm trying to do some storing and retrieving of records from a database for my app and I used some code from a thread I found here.

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/Using-HTML5-to-access-a-local-da...

 

The problem is that it doesn't work in Ripple Emulator, even though it works in Opera.

 

Here is the HTML code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
<title>Database test</title>
<script type="text/javascript">
	var db = null;

var branchOffices = [
  {"indexentry":"135","numcount":1,"cityname":"Chicago"},
  {"indexentry":"217","numcount":1,"cityname":"Los Angeles"},
  {"indexentry":"491","numcount":1,"cityname":"Waterloo"},
  {"indexentry":"854","numcount":1,"cityname":"Orlando"}
];

 
function startapp(){
// create/open database
db = openDatabase("databasetest", '1.0', 'Test', 50 * 1024);
               
db.transaction(function(tx) {
tx.executeSql("drop table branches");
tx.executeSql("CREATE TABLE IF NOT EXISTS branches(indexentry TEXT, numcount NUMBER, cityname TEXT)", [ ]);
});

db.transaction(function(tx){
  var branch = null;
  for(var i=0;i<branchOffices.length;i++){
    branch = branchOffices[i];
    tx.executeSql("INSERT INTO branches (indexentry, numcount, cityname) VALUES (?, ?, ?)", 
      [branch.indexentry, branch.numcount, branch.cityname]
    );
  }
});
}


function loaddata(){

var data1 = document.getElementById("text1").value;

db.transaction(
function(tx) {
tx.executeSql("SELECT distinct * FROM branches WHERE indexentry = ?", [data1 ],
function(tx, result) {
if (result.rows.length==1) {

document.getElementById("text2").value = result.rows.item(0).numcount + result.rows.item(0).cityname

}else{

alert(result.rows.length)
document.getElementById("text2").value = "Word could not be found."
		}
	}, null);
	});
}


</script>
</head>
<body onload="startapp();">

<form>
	<input type="text" id="text1"><br>
	<input type="text" id="text2"><br>

<input type="reset"><br>
<input type="button" id="read" onclick="loaddata();"  value="Load">
</form>


</body>
</html>

 Basically, there are two fields, enter in the ID in the first field, click on load, and it should load the data in the second field.

 

 I think it it's the tx.executesql in the loaddata function that isn't working, though I don't know why.

 

I would try to run it on the playbook simulator, but I have problems trying to package it as well....

 

Highlighted
Contributor
Posts: 33
Registered: ‎07-08-2010
My Device: Bold 9700/9900 - Playbook v2.0.xxx
My Carrier: Telus

Re: WebSQL not working on Ripple Simulator

[ Edited ]

Try adding error handlers to your transactions. You will get back some feedback on what is going wrong (alerts or console.log).

 

In ripple you can also use web inspector to check for errors (right click), examine the SQLite DB structure, and look at (not edit) the rows of the DB.

 

I started my code from this resource:

https://developer.apple.com/library/safari/#documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Us...

 

Great code example. Covers version management of your DB and upgrading, good error handling examples, almost everything I needed to get started. Make sure you look at the "A Complete Example" code.

 

I'm using the Standalone Ripple version 9.1, works great. Before I was lost as to what was going on in my apps.

 

Hope some of this helps.

 

Ross MacLachlan

Inawire Inc.

Contributor
Posts: 41
Registered: ‎02-14-2012
My Device: Curve 8900 / Playbook
My Carrier: Rogers

Re: WebSQL not working on Ripple Simulator

Alright so I figured out the problem.....

looks like ripple simulator just doesn't like how I wrote the code

 

db.transaction(function(tx) {
tx.executeSql("drop table branches");
tx.executeSql("CREATE TABLE IF NOT EXISTS branches(indexentry TEXT, numcount NUMBER, cityname TEXT)", [ ]);
});

 failed.........

 

So I rewrote it as:

db.transaction(function(tx) {
tx.executeSql("drop table branches");
});
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS branches(indexentry TEXT, numcount NUMBER, cityname TEXT)", [ ]);
});

 and it is working now.....

 

Weird how it worked the other way in Opera, but not in the ripple simulator.