在通过XML文件填充数据库和访问数据时遇到问题
Having problems filling up my database through an XML file and accesing the data
我试图解析XML文件到SQlite数据库为我的应用程序在phonegap。我遇到的问题似乎是我的数据没有保存在我的数据库中(如果我运行我的代码,我得到的日志:'代金券表:0行找到。'.
我编写的代码是基于关于存储的phonegap API。
有人看到问题在我的代码或有一些提示如何找到我的问题?
我试着/检查:
- 从xml到变量的解析工作
- 我试图让它与一个简化的表工作,得到相同的结果(无)
EDIT:openDatabase,我遇到了一个新问题。我好像只插入了最后一张代金券。为什么会这样?似乎他正在经历。each()并在完成插入语句之前重写我的变量,只是得到更改,只插入我的最后一条语句,因为这是最后一个覆盖变量的语句。
EDIT2:经过一些测试(使用console.log),我找到了问题的根源。ajax请求中的成功方法似乎没有在代码的其余部分之前运行。我猜它是异步运行的。我该如何解决这个问题?我正在考虑从ajax切换到JavaScript来解析我的XML文件。这能行吗?
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<vouchers>
<voucher>
<giftID>1</giftID>
<supplierName>supplierName1</supplierName>
<title_NL>title_NL1</title_NL>
<title_FR>title_FR1</title_FR>
<decr_NL>decr_NL1</decr_NL>
<decr_FR>decr_FR1</decr_FR>
<brands_NL>brands_NL1</brands_NL>
<brands_FR>brands_FR1</brands_FR>
<exclusion_NL>exclusion_NL1</exclusion_NL>
<exclusion_FR>exclusion_FR1</exclusion_FR>
<price_inclBTW>price_inclBTW1</price_inclBTW>
<serviceFee>serviceFee1</serviceFee>
<isEvoucher>isEvouche1r</isEvoucher>
<isFixValidDate>isFixValidDate1</isFixValidDate>
<Validtxt>Validtx1t</Validtxt>
<mainAfb>mainAf1</mainAfb>
<detailAfb1>detailAfb11</detailAfb1>
<detailAfb2>detailAfb21</detailAfb2>
<detailAfb3>detailAfb31</detailAfb3>
</voucher>
<voucher>
<giftID>2</giftID>
<supplierName>supplierName2</supplierName>
<title_NL>title_NL2</title_NL>
<title_FR>title_FR2</title_FR>
<decr_NL>decr_NL2</decr_NL>
<decr_FR>decr_FR2</decr_FR>
<brands_NL>brands_NL2</brands_NL>
<brands_FR>brands_FR2</brands_FR>
<exclusion_NL>exclusion_NL2</exclusion_NL>
<exclusion_FR>exclusion_FR2</exclusion_FR>
<price_inclBTW>price_inclBTW2</price_inclBTW>
<serviceFee>serviceFee2</serviceFee>
<isEvoucher>isEvoucher2</isEvoucher>
<isFixValidDate>isFixValidDate2</isFixValidDate>
<Validtxt>Validtxt2</Validtxt>
<mainAfb>mainAfb2</mainAfb>
<detailAfb1>detailAfb12</detailAfb1>
<detailAfb2>detailAfb22</detailAfb2>
<detailAfb3>detailAfb32</detailAfb3>
</voucher>
</vouchers>
javascript文件:
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
var db = window.openDatabase("voucher", "1.0", "Voucher database", 1000000);
db.transaction(populateDB, errorCB, successCB);
}
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS vouchers');
tx.executeSql('CREATE TABLE IF NOT EXISTS vouchers (giftID unique, supplierName, title_NL, title_FR, decr_NL, decr_FR, brands_NL, brands_FR, exclusion_NL, exclusion_FR, price_inclBTW, serviceFee, isEvoucher, isFixValidDate, Validtxt, mainAfb, detailAfb1, detailAfb2, detailAfb3)');
}
function errorCB(err) {
console.log("Error processing SQL: "+err.code);
}
// Transaction success callback
//
function successCB() {
db = window.openDatabase("voucher", "1.0", "Voucher database", 1000000);
db.transaction(queryDB, errorCB);
}
function queryDB(tx) {
tx.executeSql('SELECT giftID FROM vouchers', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("Vouchers table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).giftID);
}
}
function xmlParse() {
db = window.openDatabase("voucher", "1.0", "Voucher database",
1000000); //**ADDED THIS**
$.ajax({
type: "GET",
url: "testVoucher.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('voucher').each(function() {
giftID = $(this).find("giftID").text();
supplierName = $(this).find("supplierName").text();
title_NL = $(this).find("title_NL").text();
title_FR = $(this).find("title_FR").text();
decr_NL = $(this).find("decr_NL").text();
decr_FR = $(this).find("decr_FR").text();
brands_NL = $(this).find("title_FR").text();
brands_FR = $(this).find("title_FR").text();
exclusion_NL = $(this).find("exclusion_NL").text();
exclusion_FR = $(this).find("exclusion_FR").text();
price_inclBTW = $(this).find("price_inclBTW").text();
serviceFee = $(this).find("serviceFee").text();
isEvoucher = $(this).find("isEvoucher ").text();
isFixValidDate = $(this).find("isFixValidDate").text();
Validtxt = $(this).find("Validtxt").text();
mainAfb = $(this).find("mainAfb").text();
detailAfb1 = $(this).find("detailAfb1").text();
detailAfb2 = $(this).find("detailAfb2").text();
detailAfb3 = $(this).find("detailAfb3").text();
db.transaction(function(tx) {
tx.executeSql('INSERT INTO vouchers (giftID, supplierName, title_NL, title_FR, decr_NL, decr_FR, brands_NL, brands_FR, exclusion_NL, exclusion_FR, price_inclBTW, serviceFee, isEvoucher, isFixValidDate, Validtxt, mainAfb, detailAfb1, detailAfb2, detailAfb3) VALUES (' + giftID + ',' + supplierName + ',' + title_NL + ',' + title_FR + ',' + decr_NL + ',' + decr_FR + ','
+ brands_NL + ',' + brands_FR + ',' + exclusion_NL + ',' + exclusion_FR + ',' + price_inclBTW + ',' + serviceFee + ','
+ isEvoucher + ',' + isFixValidDate + ',' + Validtxt + ',' + mainAfb + ',' + detailAfb1 + ',' + detailAfb2 + ',' + detailAfb3 + ')');
});
});
}
}
);
}
在您的xmlParse
函数中,db
未定义。在交易之前添加这一行:
db = window.openDatabase("voucher", "1.0", "Voucher database", 1000000);
::更新::
试着像下面这样修改ajax调用。我正在创建您的值的数组,然后将数组传递给插入语句:
$.ajax({
type: "GET",
url: "testVoucher.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('voucher').each(function() {
var voucher = [];
voucher.push($(this).find("giftID").text());
voucher.push($(this).find("supplierName").text());
voucher.push($(this).find("title_NL").text());
voucher.push($(this).find("title_FR").text());
voucher.push($(this).find("decr_NL").text());
voucher.push($(this).find("decr_FR").text());
voucher.push($(this).find("title_FR").text());
voucher.push($(this).find("title_FR").text());
voucher.push($(this).find("exclusion_NL").text());
voucher.push($(this).find("exclusion_FR").text());
voucher.push($(this).find("price_inclBTW").text());
voucher.push($(this).find("serviceFee").text());
voucher.push($(this).find("isEvoucher").text());
voucher.push($(this).find("isFixValidDate").text());
voucher.push($(this).find("Validtxt").text());
voucher.push($(this).find("mainAfb").text());
voucher.push($(this).find("detailAfb1").text());
voucher.push($(this).find("detailAfb2").text());
voucher.push($(this).find("detailAfb3").text());
console.log(voucher);
db.transaction(function(tx) {
tx.executeSql('INSERT INTO vouchers (giftID, supplierName, title_NL, title_FR, decr_NL, decr_FR, brands_NL, brands_FR, exclusion_NL, exclusion_FR, price_inclBTW, serviceFee, isEvoucher, isFixValidDate, Validtxt, mainAfb, detailAfb1, detailAfb2, detailAfb3) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',voucher);
});
});
}
}
);
相关文章:
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Wordpress'RESTApi访问本地主机上的数据
- 访问令牌和响应数据
- 如何访问高图表工具提示中的任何特定数据
- 将JSON传递给Jade,并使用相同的数据进行API访问
- 访问Protractor/E2E测试中的$http数据(AngularJS)
- 使用Angular.js访问php数组数据
- 访问Twilio短信元数据
- 访问通过XHR.onreadystatechange回调中的XHR.send(data)发送的数据
- Codeigniter Jquery Ajax,Can't将控制器中的数据访问回html表单
- JavaScript IE 从 json 文件中读取数据 访问被拒绝
- 将twitter api1.0迁移到1.1 json数据访问问题
- 管理数据访问/用户权限
- Javascript json数据访问行和字段
- Javascript是否实现了类似SQL的数据访问
- 在猫鼬之上创建一个额外的数据访问层
- 角度.js数据访问器
- JavaScript数据访问设计
- 在bogart中使用承诺数据访问
- 在初始.ajax调用之外使用JSON数据-访问剩余的JSON数据