HTML5SQL retrieve records from web databases using SELECT statements

笑着哭i 提交于 2019-12-07 18:50:24

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8171168/html5sql-retrieve-records-from-web-databases-using-select-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!