How to retrieve value from database and display it in an html5 using (jqtouch+phonegap)

99封情书 提交于 2020-01-14 05:46:09

问题


I am trying to make an Iphone application for mobile health using html5,javascript, jqtouch and phonegap. I am doing this as a school project to learn building an iPhone app using html5 jqtouch and phonegap.

I am able to create a database with pName as a column in the database table. But I am unable to populate the entire patient name list in an empty div(first pannel named patientList)in index.html.

I have made one file named index.html. This file has different pannels. The first pannel is a patientList pannel. The second pannel is to create a new entry in a database. Once I create a new entry in the database, the first pannel named patient list should populate all the names of the patient. My code creates a database successfully but it does not show any name of the patients(pName) in the PatientList pannel.

I am making iphone app using HTML5, CSS, JAVASCRIPT, AND JQTOUCH, AND PHONEGAP for the first time. I need your help.

My index.html looks like this

<div id="patientList">
      <div class="toolbar">
          <h1>patientList</h1>
          <a class="button slideup" href="#newEntry">+</a>
      </div>
      <ul class="edgetoedge">

          <li id="entryTemplate" class="entry" style="display:none">
              <span class="label">Label</span>

          </li>

      </ul>
  </div>
  <div id="newEntry">
      <div class="toolbar">
          <a class="button cancel" href="#">Cancel</a>
          <h1>New Patient</h1>
          <a class="button save" href="#">Save</a>

      </div>

      <br/>

       <form method="post" >
          <ul class="rounded">
              <li><input type="text" placeholder="Patient Name" 
                  name="PatientName" id="PatientName" autocapitalize="off" 
                  autocorrect="off" autocomplete="off" /></li>
          </ul>
             <a class="button add" onclick="addNewMedicine()"style="size:12">+</a> 
          </ul>
           <div id="empty" class="rounded">
           </div>
          <ul class="rounded">
              <li><input type="submit" class="submit" name="action" 
                  value="Save Entry" /></li>
          </ul>
      </form>
  </div>

My iphone.js looks like this

     var jQT = new $.jQTouch({
           });

  var db;

    $(document).ready(function(){
       $('#newEntry form').submit(createEntry);
       $('#patientList li a').click(function(){
       var nameOffset = this.id;
       sessionStorage.currentName = nameOffset; // storing the clicked patient name
       refreshEntries();
      });

   // creating a database with name PatientDataBase and which has the table named patientRecord1

   var shortName = 'patientDataBase';
    var version = '1.0';
    var displayName = 'patientDataBase';
    var maxSize = 65536;
    db = openDatabase(shortName, version, displayName, maxSize);
    db.transaction(
       function(transaction) {
          transaction.executeSql(
             'CREATE TABLE IF NOT EXISTS patientRecord1 ' +
            '  (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
            '  pName TEXT NOT NULL);'
              );
           }
        );
     });


  // Function created a new enty in the database table patientRecord1 
   function createEntry() {
         var pName = $('#PatientName').val();
         db.transaction(
         function(transaction) {
         transaction.executeSql(
         'INSERT INTO patientRecord1 (pName) VALUES (?);', 
          [pName], 
          function(){
           refreshEntries();
           jQT.goBack();
         }, 
        errorHandler
       );
     }
   );
    return false;
  }
      // this function is used to retrive the data from the table and populate in the html pannel named patientList

     function refreshEntries() {
      $('#patientList ul li:gt(0)').remove();
     db.transaction(
     function(transaction) {
          transaction.executeSql(
          'SELECT * FROM patientRecord1;', 
                 function (transaction, result) {
                 for (var i=0; i < result.rows.length; i++) {
                      var row = result.rows.item(i);
                      var newEntryRow = $('#entryTemplate').clone();
                      newEntryRow.removeAttr('id');
                      newEntryRow.removeAttr('style');
                      newEntryRow.data('entryId', row.id);
                      newEntryRow.appendTo('#patientList ul');
                     newEntryRow.find('.label').text(row.pName);
                  }
               }, 
       errorHandler
         );
     }
    ); 
   }

        function errorHandler(transaction, error) {
         alert('Oops. Error was '+error.message+' (Code '+error.code+')');
         return true;
     }

Please tell me where I am doing wrong.


回答1:


Replace your refreshEntries() function with following:

function refreshEntries() {
    $('#patientList ul li:gt(0)').remove();
    db.transaction(

    function(transaction) {
        transaction.executeSql('SELECT * FROM patientRecord1;', [], function(transaction, result) {
            for (var i = 0; i < result.rows.length; i++) {
                var row = result.rows.item(i);
                var newEntryRow = $('#entryTemplate').clone();
                newEntryRow.removeAttr('id');
                newEntryRow.removeAttr('style');
                newEntryRow.data('entryId', row.id);
                newEntryRow.appendTo('#patientList ul');
                newEntryRow.find('.label').text(row.pName);
            }
        }, errorHandler);
    });
}

You have missed a param array in argument of executeSql. I have put the new code in a fiddle here



来源:https://stackoverflow.com/questions/9777992/how-to-retrieve-value-from-database-and-display-it-in-an-html5-using-jqtouchp

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