在通过XML文件填充数据库并访问数据时遇到问题

时间:2014-03-11 15:38:55

标签: javascript ajax xml sqlite cordova

我试图将一个XML文件解析为SQlite数据库,用于我在phonegap中的应用程序。 我遇到的问题似乎是我的数据没有保存在我的数据库中(如果我运行我的代码,我就得到了日志:'优惠券表:找到0行。'

我制作的代码基于phonegap API存储空间。

有人在我的代码中看到问题或者有一些提示如何找到我的问题吗?

我试过/检查过:

  1. 从xml到变量的解析工作
  2. 我试图让它使用简化表,得到相同的结果(无)
  3. 编辑:添加db.openDatabase后,我遇到了新问题。我似乎只插入了我的最后一张优惠券。为什么是这样?看起来他正在通过.each()并在完成insert语句之前重写我的变量,只是将更改只插入我的最后一个语句,因为那是最后一次覆盖变量的那个。

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

1 个答案:

答案 0 :(得分:0)

在您的xmlParse函数db中未定义。在您的交易之前添加以下行:

db = window.openDatabase("voucher", "1.0", "Voucher database", 1000000);

::修订::

尝试修改您的ajax调用,如下所示。我正在创建一个值数组,然后将数组传递给insert语句:

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

    }
);