So here is what our code looks like:
We needed to use this code at two different places on the application and so pulled it out into it's own class. The initialize function does just what you would think it would do, and the beginning of the new interesting stuff is on line 4
This openDatabase command is a new HTML5 window method, so it starts to get interesting. This method takes 4 things:this.db = openDatabase("Database Name", "1.0", "Display Name", 10000);
- The database name: here brillantly named, Database Name (I didn't say it had to be interesting)
- The version of the database: 1.0
- The display name: Display Name (again....interesting naming isn't the game)
- The estimated size in bytes of data that will be stored in the database: 10000
The two things we did were to call a transaction on the database object and then execute some SQL statements on that transaction. All the interesting happens in these lines:
In general to execute the SQL you do this:
Where the function(tx,result){} is going to hold the action of what you want to do if the SQL instructions passed and likewise the function(tx, error){} will hold the instructions of what to do if your SQL fails. In our case the error actions are just logging the error messages while the successes are dealing with figuring out if the user has been to the page before.
We didn't get any deeper into the HTML5 database things, but being able to create databases and check for users on the fly was a fun and exciting find.