简体   繁体   English

Phonegap / Jquery mobile将XML读入数据库,然后在div中显示数据

[英]Phonegap/Jquery mobile Read an XML into a database and then display the data in a div

I am looking to access the information that I have stored in the databse that Ive parsed out of an XML. 我正在寻找访问Ive从XML解析出来的数据库中存储的信息。 I am using phonegap (cordova 2.2.0). 我正在使用phonegap(cordova 2.2.0)。 I am calling queryDB() on click on my navigation bar to call the function to populate the div with the information that was parsed from an XML. 我在导航栏上单击以调用queryDB()来调用该函数,以用从XML解析的信息填充div。 My issues is that the information is not being displayed. 我的问题是该信息未显示。

Anyone have any idea why? 有人知道为什么吗? Below is my code: 下面是我的代码:

On click 点击时

<a href="#acceptedOrders" onclick("queryDB();")> <img src="Images/acceptedOrders.gif" width="25%"> </a>

This is a javascript function that reads an XML into a databse: 这是一个将XML读入数据库的javascript函数:

var db = openDatabase('PhoneGap_db', '1.0', '', 2 * 1024 * 1024);
db.transaction(function (tx) {
  tx.executeSql('CREATE TABLE IF NOT EXISTS orders (id unique, city, state, zip)');
});


db.transaction(function (tx) {
   tx.executeSql('INSERT INTO orders (id, city, state, zip) VALUES (orderId,city,state,zip)');
});

$.ajax({
    type: "GET",
    url: "testOrders.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('order').each(function(){
        orderId = $(this).find("orderId").text();
        city = $(this).find("city").text();
        state = $(this).find("state").text();
        zip = $(this).find("zip").text();
        db.transaction(function (tx) {
           tx.executeSql('INSERT INTO orders (id, city, state, zip) VALUES ('+ orderId +', '+ city + ', ' + state + ', ' + zip + ')');
        });
            });
        }
     }); 

Here is my Read Script(not working) 这是我的阅读脚本(不起作用)

function queryDB(tx) {
  tx.executeSql('SELECT orderId FROM orders', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
  var len = results.rows.length;
  for (var i=0; i<len; i++){
      $('#acceptedOrdersContent').append('<li><a href="#CompleteOrderInfo">'+results.rows.item(i).orderId+'</a></li>').listview("refresh");
  }
};
function errorCB(err) {
  console.log("Error processing SQL: "+err.code);
}

This is an example XML file: 这是一个示例XML文件:

    <?xml version="1.0" encoding="UTF-8"?>
     <orders>
        <order>
         <orderId>123456789</orderId>
         <city>Cincinnati</city>
         <state>Ohio</state>
        <zip>45451</zip>
    </order>
 </orders> 

Thanks everyone! 感谢大家!

First of all, your html is wrong, you should use onclick="" instead of onclick(""), like this: 首先,您的html是错误的,您应该使用onclick =“”代替onclick(“”),如下所示:

<a href="#acceptedOrders" onclick="queryDB();">
  <img src="Images/acceptedOrders.gif" width="25%">
</a>

Also your queryDB functions is expecting a parameter that's not being passed. 另外,您的queryDB函数期望未传递的参数。 You should update it to something like this: 您应该将其更新为以下内容:

function queryDB() {
  db.transaction(function (tx) {
    tx.executeSql('SELECT orderId FROM orders', [], querySuccess, errorCB);
  });
}

Keep in mind that this should be in the same context where db object was created 请记住,这应该与创建db对象的环境相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM