javascript - Extracting data from Phonegap's SQLite database - Stack Overflow

I am using a SQLite database in my PhoneGap application. I am able to populate the database, extract th

I am using a SQLite database in my PhoneGap application. I am able to populate the database, extract the database and also print it using console.log

Here is the code

function extractFromDB(){
     var db = window.openDatabase("hospitalsDB", "1.0", "HospitalsDB", false);
     db.transaction(queryDB, errorCB);
}

  function queryDB(tx) {
     tx.executeSql('SELECT * FROM hospitals', [], querySuccess, errorCB);
 }

 function querySuccess(tx, results) {
     var len = results.rows.length;
     console.log("DEMO table: " + len + " rows found.");
     for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
      }
}

My question is, how do I print this data onto the html page ?

I am using a SQLite database in my PhoneGap application. I am able to populate the database, extract the database and also print it using console.log

Here is the code

function extractFromDB(){
     var db = window.openDatabase("hospitalsDB", "1.0", "HospitalsDB", false);
     db.transaction(queryDB, errorCB);
}

  function queryDB(tx) {
     tx.executeSql('SELECT * FROM hospitals', [], querySuccess, errorCB);
 }

 function querySuccess(tx, results) {
     var len = results.rows.length;
     console.log("DEMO table: " + len + " rows found.");
     for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
      }
}

My question is, how do I print this data onto the html page ?

Share Improve this question asked Mar 24, 2013 at 0:03 Ashish AgarwalAshish Agarwal 14.9k31 gold badges88 silver badges126 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

If i get your question in right way.Assume you have a <div> in your html page:

var resultDiv = $('#mydiv');
var data = undefined;
for (var i=0; i<len; i++){
     data = "Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data;
     resultDiv.append(data + "<br>")

This will write all the data into a div in your html page. A simple example smilar to your one: JSFIDDLE

function querySuccess(tx, results) {
  <!-- its good programming to check the length of rows returned by sqlite DB.-->
     var htmlstring = "";
         if (results != null && results.rows != null && results.rows.length > 0) {
                for ( var i = 0;i <results.rows.length;i++)
                    {
    htmlstring+= '<div class="ui-grid-a">';
          htmlstring += '<div class="ui-block-a">'+results.rows.item(i).id+'</div>'; 
           htmlstring+= '</div>';  

                   }

$("#divholderfordata").empty().append(htmlstring).trigger('create');

          }
   else
 {console.log("no records inserted in DB");}
}

where you can define "div" with id "divholderfordata" in your html code this code will work better as far as "UI" is concerned because it also includes "jquery-mobile" code hope it will help you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257536a4619045.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信