【问题标题】:How to retrieve value from database and display it in an html5 using (jqtouch+phonegap)如何使用 (jqtouch+phonegap) 从数据库中检索值并将其显示在 html5 中
【发布时间】:2012-03-19 21:10:00
【问题描述】:

我正在尝试使用 html5、javascript、jqtouch 和 phonegap 制作一个用于移动健康的 Iphone 应用程序。我正在做这个作为一个学校项目来学习使用 html5 jqtouch 和 phonegap 构建一个 iPhone 应用程序。

我可以在数据库表中创建一个以 pName 作为列的数据库。但我无法在 index.html 中的空 div(名为 PatientList 的第一个面板)中填充整个患者姓名列表。

我制作了一个名为 index.html 的文件。这个文件有不同的面板。 第一个面板是一个 PatientList 面板。第二个面板是在数据库中创建一个新条目。 在数据库中创建新条目后,名为患者列表的第一个面板应填充患者的所有姓名。我的代码成功创建了一个数据库,但它没有在 PatientList 面板中显示任何患者姓名(pName)。

我是第一次使用 HTML5、CSS、JAVASCRIPT、JQTOUCH 和 PHONEGAP 制作 iphone 应用程序。我需要你的帮助。

我的 index.html 是这样的

<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>

我的 iphone.js 是这样的

     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;
     }

请告诉我哪里做错了。

【问题讨论】:

  • 只是一个建议。你为什么要深入了解 sql 的本质。使用像草坪椅这样的包装纸来为你处理。只需保存 json 并检索它。

标签: jquery database html cordova jqtouch


【解决方案1】:

将您的 refreshEntries() 函数替换为以下内容:

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);
    });
}

您在executeSql 的参数中遗漏了一个参数数组。我把新代码放在了小提琴here

【讨论】:

  • 非常感谢达瓦尔。在您回复后,我可以在 PatientList 上看到我的 pName 列表。你是绝对正确的,我只是缺少 executeSql 参数中的参数数组。我刚刚添加了,[],一切都开始工作了。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 2014-06-15
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多