Question

I am creating an iphone webapp using jquery mobile/phonegap and I am trying to use web database. I have about 700 records to insert and want to have a separate file with the sql statements to create the table and insert the records the fist time the app runs. I decided to use an small javascript module called html5sql because It gives me the advantage of executing several sql statements in one single transaction.

for example I can execute

sql = "CREATE TABLE milestones ( 
       ID       INT( 10 )        NOT NULL,
       Title    VARCHAR( 50 )    DEFAULT NULL,
       mYear    INT( 11 )        NOT NULL);
       INSERT INTO `milestones` VALUES ('2', 'Cotton Mather', '1721');";

using

 html5sql.process(sql, function(){ //Success}, function(error, failingQuery){//Failure});

instead of

transaction.executeSql('CREATE TABLE milestones (ID INT( 10 ) NOT NULL, Title VARCHAR(50 )    DEFAULT NULL, mYear    INT( 11 )        NOT NULL);', [], nullDataHandler, errorHandler); 
transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear]) VALUES (2, "Cotton Mather", "1721");', [], nullDataHandler, errorHandler); 

The problem of htlm5sql is that they don't have any information on how to retrieve information from the database using the process function and SELECT statements and display the results in a div block.

here is the only short guide for html5sql

Can anyone help me with this?

Was it helpful?

Solution

Sure. Selecting data with html5sql is really quite simple and can be accomplished with something as simple as:

html5sql.process(
    ["SELECT * FROM milestones;"],
    function(transaction, results, rowsArray){
      for(var i = 0; i < rowsArray.length; i++){
        //each row in the rowsArray represents a row retrieved from the database
        var id = rowsArray[i].ID;
        var Title = rowsArray[i].Title;
        var mYear = rowsArray[i].mYear;
        console.log("Retrieved Milestone: "+id+" - "+Title+" "+mYear);
      }
    },
    function(error, statement){
      //hande error here           
    }
);

If you want a more in-depth example I created a jsfiddle a while back which has actual working code showing the whole process of creating and populating a database and then selecting and retrieving data.

As the author of html5sql I apologize for the lack of documentation. I am working to improve the documentation on the site and add more live examples of how to do things.

OTHER TIPS

This took me forever to figure out. You should really add that 3rd param for select results in your docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top