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
Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: POST/READ data to PHP/MYSQL on webserver

Hello Justam,

 

Sounds like you're describing focus-based navigation:

https://developer.blackberry.com/html5/documentation/ww_developing/Using_the_Navigation_Mode_1866978...

 

Also, congrats on the progress thus far! Based on the topics noted earlier, it wouldn't be a small feat to implement such an app.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

[ Edited ]

Hello Erik,

 

That's perfect focused based navigation. I did encounter an issue with using localStorage, it crashes in certain os6 dot revisions so I'm going to have to use another method. I see that html5 database is os5+ compatible. I am a little confused as how to use this ie: setting up dB initially/ check if dB exists and reading/writing data. Is it possible for you to provide to me a rough example of creating/checking if dB exists and storing and reading data. I am only storing 3 pieces of data, license, record number and a base64 string. I did read the database example on github and I'm still confused :s

 

I was there thinking of saving a file to the file system and encrypting its contents and then reading that file in and decrypting the contents but that isn't as secure or as efficient as using the dB.

 

Thanks for your support.

 

 

Regards, Justam

 

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: POST/READ data to PHP/MYSQL on webserver

Hello Justam,

 

The API reference probably provides the simplest overview of the APIs:

https://bdsc.webapps.blackberry.com/html5/apis/Database.html

 

The basic approach would be to:

 

  1. Open / create the database.
  2. Create the table you want to use if it does not exist.
  3. Populate/read from/modify the table.

To open / create the database, you can call the following:

 

mynamespace.db = window.openDatabase('DB_NAME', 'DB_VERSION', 'DB_DESCRIPTION', 5 * 1024 * 1024, onDBCreate);

 

Prior to this call, we would have had to define mynamespace as follows:

 

if (typeof mynamespace === 'undefined') {
    mynamespace = {};
}

 

Also, note the reference to onDBCreate. This is another function that will be invoked if we successfully create the database. Generally, this is where we do our table creation the first time around. Something along the lines of:

 

function onDBCreate (database) {
    //Attach the database because "window.openDatabase" would not have returned it
    mynamespace.db = database;

    //Create the table in the database
    database.transaction (
        function (tx) {
            var query = 'CREATE TABLE IF NOT EXISTS tbl_name (key int unique, name text)';
            tx.executeSql(
                query,
                [],
                function (tx, res) {
                    alert("Table Created Successfully");
                },
                function (tx, err) {
                    alert("ERROR - Table creation failed - code: " + err.code + ", message: " + err.message);
                }
            );
        }
    );
}

 

Here, when the database is created, we immediately store the database as a member of our namespace. We then execute a transaction to create our main table if it does not exist. In our case, we create a unique, integer key field, and a second name field that is text.

 

From here on out, database.transaction is king. Just about everything will rely on constructing and executing the appropriate query (i.e. SELECT, UPDATE, DELETE, etc.) The following is a pretty good rundown of various transactions, though they omit success/failure callbacks:

http://www.tutorialspoint.com/html5/html5_web_sql.htm

 

To each query that is executed, we can also attach a success and failure callback if we need to handle those events. A more fully developed sample is also available in our WebWorks ECL KB:

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/Sample-Emergency-Contact-List-Ap...

 

The above is a push sample that integrates with an HTML5 database. A good place to get a look at some additional queries and usage.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

Hello Erik, 

 

Thank you for the reply and clearing up some questions I had. I use mysql within my php all the time and was just finding it odd I couldn't wrap my head around a few things. Based on what you have provided I wrote the below functions to test but they dont seem to work...I get the first alert on each function but then nothing after....  

 

<!DOCTYPE html>
<html>
<head>

<title></title>
<script type="text/javascript">
var serial;
var uid;
var base64;
var isPass;
var pass;
if (typeof db === 'undefined') {
    db = {};
}
function readDb(){
alert("1");
mynamespace.db = database;
	var db = window.openDatabase('bgd', '1.0', 'accountinfo', 5 * 1024 * 1024);
	db.transaction(function (tx) {
	tx.executeSql('SELECT * FROM account', [], function (tx, results) {
	alert(results.rows.length);
	var len = results.rows.length, i;
	for (i = 0; i < len; i++){
		/*uid = results.rows.item[0].uid;
		serial = results.rows.item[0].serial;
		base64 = results.rows.item[0].base64;
		isPass = results.rows.item[0].ispass;
		pass = results.rows.item[0].pass;*/
		alert(results.rows.item[0].uid);
		alert(results.rows.item[0].serial);
		alert(results.rows.item[0].base64);
		alert(results.rows.item[0].ispass);
		alert(results.rows.item[0].pass);
   }
 }, null);
});
alert(uid);
alert(serial);
alert(base64);
alert(isPass);
alert(pass);

}

function addAccount(){
alert("2");
serial = "1234";
uid = "5678";
base64 = "312dsae324tgdf";
mynamespace.db = database;
	var db = window.openDatabase('bgd', '1.0', 'accountinfo', 5 * 1024 * 1024);
	db.transaction(function (tx) {
		tx.executeSql('CREATE TABLE IF NOT EXISTS account (serial text, uid text, base64 blob, ispass int, pass text)');
		tx.executeSql('INSERT INTO account (serial, uid, base64, ispass) VALUES (serial, uid, base64, "0")');
	});

alert(uid);
alert(serial);
alert(base64);
}
function storePass(){
alert("3");
	uid = "5678";
	mynamespace.db = database;
	pass = "password";
	var db = window.openDatabase('bgd', '1.0', 'accountinfo', 5 * 1024 * 1024);
	db.transaction(function (tx) {
	   tx.executeSql('UPDATE account WHERE uid = uid (ispass, pass) VALUES ("1", pass)');
	});
}
</script>
</head>
<body>
<div><button onClick="addAccount();">Add</button></div>
<div><button onClick="readDb();">Read</button></div>
<div><button onClick="storePass();">Password</button></div>
</body>
</html>

 

Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

I have also tried this and it is not working either...

if (typeof mynamespace === 'undefined') {
    mynamespace = {};
}
mynamespace.db = window.openDatabase('bgd', '1.0', 'account information', 5 * 1024 * 1024, onDBCreate);

(function () {

    //This method is only called once (the first time the database is created)
    function onDBCreate(database) {
	
        //Attach the database because "window.openDatabase" would not have returned it
        mynamespace.db = database;
        //Create the table in the database
        database.transaction(
            function (tx) {tx.executeSql('CREATE TABLE IF NOT EXISTS account (id text, serial text, uid text, base64 blob, ispass int, pass text)',
                [],
                function (tx, res) {
                    alert("Table Created Successfully");
                },
                function (tx, err) {
                    alert("ERROR - Table creation failed - code: " + err.code + ", message: " + err.message);
                });
            }
        );
    }

    if (window.openDatabase) {
        //Will either return the existing database or null and call our creation callback onDBCreate
        mynamespace.db = window.openDatabase('bgd', '1.0', 'account information', 5 * 1024 * 1024, onDBCreate);
    } else {
        alert("This device does not have HTML5 Database support");
    }
}());

 

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: POST/READ data to PHP/MYSQL on webserver

Hello Justam,

 

Perhaps the sample here will help clear up some things:

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/OS5-OS6-Ripple-and-Chrome-Databa...

 

Note, there are a few references to javascript&colon; that should simply be javascript&colon; however the encoding on these forums forced it into the long-form.

 

The sample itself contains two HTML pages. Each page links to the other. Each page also gives a list of controls:

  • Open Unique Database
  • Create Table
  • Select Items
  • Insert Item
  • Drop Table

Each <a> element triggers a separate function in scripts.js.

 

Note that:

 

if (typeof mynamespace === 'undefined') {
	mynamespace = {};
}

Is not contained within a function, so when scripts.js is loaded, mynamespace will be created so that it can be used by the remaining functions.

 

There is no additional "onload" functionality, everything else is driven through the <a> elements. However, if we wanted some specific "onload" functionality, we could call window.addEventListener and register a DOMContentLoaded event that triggers onDBCreate,. This would, at least, create / open the database when our page loads, preventing us from having to click that first link to enable functionality.

 

I would recommend copying the code to your preferred editor so that the formatting is a little more clear.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

Thank you, for your reply and the information you have provided. I will go through the sample code thoroughly to gain a better understanding of these processes. Thank you for your support again.
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

[ Edited ]

Hello Erik, 

 

 Also, since I have added those 3 .js files this is not working anymore:

 

blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,
	function() {
	   history.back();
	   return false;
	});

If I navigate from index.html to about.html and press back it closes the app....and when I'm on index.html back doesn't close the app like it use to...

 

Also if I load options.html and want to write to the database from there do I need to reinitialise the db again using openUDB() ?

 

Thanks again for your time.

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: POST/READ data to PHP/MYSQL on webserver

Hello Justam,

 

Each page load will destroy any associated JavaScript objects with that page, so when you switch pages, if you want to access the database there, you will need to re-open that database. Generally, best practices have shifted to having only one HTML page ever, and loading in specific content dynamically.

 

For instance, having a <div id="content"></div>, and then using Ajax to fill it. This will prevent your JavaScript Object from being destroyed between "page changes". I have a feeling that the onHardwareKey script is only being executed on your index page, and not the about page. This would account for the behaviour on the about page.

 

On the index page, history.back() may be failing, thus preventing the rest of the script from executing. Have you happened to hook up a Web Inspector to see whether any errors are being thrown in the console?


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
Posts: 58
Registered: ‎02-14-2011
My Device: Bold 9700
My Carrier: Rogers

Re: POST/READ data to PHP/MYSQL on webserver

[ Edited ]

Placing all my content in 1 html using <div id="contentname">...is the same conclusion I ended up coming to aswell. As for the history.back() issue it was a coding error which I resolved.

 

I have been trying to figure out how I can add a contact to the address book from a vcf file which will be downloaded to the device. While looking through the api I found this:

 

<script type="text/javascript">  
  var contact = new blackberry.pim.Contact();
  contact.firstName = 'Nick';
  contact.lastName = 'Scott';
  
  var args = new blackberry.invoke.AddressBookArguments(contact);
  args.view = 0;
   
  blackberry.invoke.invoke(blackberry.invoke.APP_ADDRESSBOOK, args);  
</script>

 

Now I could read the contents of the *.vcf file and create a contact like the above method. Is there documentation which specifies all of the entries I can populate and the correct arguments, eg: 

 

  contact.lastName='some last name';

 OR alternatively can I launch or invoke the .vcf file from the local file system? Maybe using the invoke browsers and pointing to file:///SDCard...?