我的websql事务将不会执行

时间:2011-08-11 12:17:34

标签: javascript json html5 jquery-mobile web-sql

我在HTML5中有一个使用websql数据库的移动应用程序。我有一个data.js文件,其中包含一系列用于与数据库进行各种CRUD作业的函数。在我接通这个功能之前,我从未遇到过这个问题。基本上,该应用程序用于为商人创建报价,我正在编写的函数是获取引用和引用行,将它们转换为JSON对象并将它们转换为Web应用程序。

由于某些原因,我的db.transaction没有被执行。你能帮我搞清楚为什么吗? db存在,因为其他函数正在调用这个确切的SQL。但它对他们有用而不是这个:

function syncQuote(){
    var quote_id = localStorage["quote_id"];

    var header = new Array(); // holds id, created, appointment_id

    db = openDatabase("Quote", "0.1", "A collection of quotes.", 200000);

    if(!db){
        console.log('Failed to connect to database.');
    }

    console.log('Getting quote header data.');
    db.transaction(
        function(tx) {
            tx.executeSql("SELECT * FROM quote_header WHERE id = ?", [quote_id],
                function(tx, results) {

                    var len = results.rows.length;
                    for(var i=0; i< len; i++){
                        alert('booyah!');
                        header['quote_id'] = results.rows.item(i).id;
                        header['appointment_id'] = results.rows.item(i).appointment_id;
                        header['created'] = results.rows.item(i).created;
                    }
                });
        },
        function(tx, error){
            console.log(error.message);
        }
        );

     // now get all quote lines for this quote

     var lines = new Array();
     console.log('getting quote lines');
     db.transaction(
        function(tx) {
            tx.executeSql("SELECT DISTINCT areas.label as area, features.label as feature, products.label as product, hours, price, colour FROM quote_line JOIN areas ON quote_line.area_id = areas.area_id JOIN features ON quote_line.feature_id = features.feature_id JOIN products ON quote_line.product_id = products.product_id WHERE quote_line.quote_id = ?", [quote_id],
                function(tx, results) {

                    len = results.rows.length;
                    for(var i=0; i< len; i++){
                        var area= results.rows.item(i).area;
                        var feature= results.rows.item(i).feature;
                        var product= results.rows.item(i).product;
                        var hours= results.rows.item(i).hours;
                        var price= results.rows.item(i).price;
                        var colour= results.rows.item(i).colour;

                        lines[i] = new Array(6);
                        lines[i][0] = area;
                        lines[i][1] = feature;
                        lines[i][2] = product;
                        lines[i][3] = hours;
                        lines[i][4] = price;
                        lines[i][5] = colour;

                    }

                },

                function(tx, error){
                    console.log(error.message);
                }
            );
        }
    );

    var data = new Array(2);
    data[0] = JSON.stringify(header);
    data[1] = JSON.stringify(lines);

    alert(data[0]);
    alert(data[1]);

    // post data to web app
    var url = "http://*****.com/import_quote";

    $.ajax({
      type: 'POST',
      url: url,
      data: data,
      success: quote_sync_success,
      dataType: 'JSON'
    });

}

我有成功和失败的回调,但都没有响应。

这也是我第一次从JS应用程序发布JSON,所以请随意发表评论。

谢谢,

比利

2 个答案:

答案 0 :(得分:1)

复制此编辑的代码并查看其是否有效

function syncQuote(){
    var quote_id = localStorage["quote_id"];

    var header = new Array(); // holds id, created, appointment_id

    db = openDatabase("Quote", "0.1", "A collection of quotes.", 200000);

    if(!db){
        console.log('Failed to connect to database.');
    }

    console.log('Getting quote header data.');
    db.transaction(
        function(tx) {

        // CORRECTION 1: THE ? IS MEANT TO BE IN A BRACKET
            tx.executeSql("SELECT * FROM quote_header WHERE id = (?)", [quote_id],
                function(tx, results) {

                    var len = results.rows.length;
                    for(var i=0; i< len; i++){
                        alert('booyah!');
                        header['quote_id'] = results.rows.item(i).id;
                        header['appointment_id'] = results.rows.item(i).appointment_id;
                        header['created'] = results.rows.item(i).created;
                    }
                });
        },
        function(tx, error){
            console.log(error.message);
        },

    //CORRECTION 2  
    //THERE IS MEANT TO BE A SUCCESS CALL BACK FUNCTION HERE    
    function(){
        console.log( 'Query Completed' )
    }       

        );

     // now get all quote lines for this quote

     var lines = new Array();
     console.log('getting quote lines');
     db.transaction(
        function(tx) {

                // CORRECTION 3: WRONG CALL METHOD AND NONE-USE OF BRACKETS and QOUTES
            tx.executeSql("SELECT DISTINCT areas.label as area, features.label as feature, products.label as product, hours, price, colour FROM quote_line JOIN areas ON quote_line.area_id = 'areas.area_id' JOIN features ON quote_line.feature_id = 'features.feature_id' JOIN products ON quote_line.product_id = 'products.product_id' WHERE quote_line.quote_id = (?)", [quote_id],
                function(tx, results) {

                    len = results.rows.length;
                    for(var i=0; i< len; i++){
                        var area= results.rows.item(i).area;
                        var feature= results.rows.item(i).feature;
                        var product= results.rows.item(i).product;
                        var hours= results.rows.item(i).hours;
                        var price= results.rows.item(i).price;
                        var colour= results.rows.item(i).colour;

                        lines[i] = new Array(6);
                        lines[i][0] = area;
                        lines[i][1] = feature;
                        lines[i][2] = product;
                        lines[i][3] = hours;
                        lines[i][4] = price;
                        lines[i][5] = colour;

                    }

                },

                function(tx, error){
                    console.log(error.message);
                }
            );
        }
    );

    var data = new Array(2);
    data[0] = JSON.stringify(header);
    data[1] = JSON.stringify(lines);

    alert(data[0]);
    alert(data[1]);

    // post data to web app
    var url = "http://*****.com/import_quote";

    $.ajax({
      type: 'POST',
      url: url,
      data: data,
      success: quote_sync_success,
      dataType: 'JSON'
    });
}

答案 1 :(得分:0)

您是否尝试在成功回调开始时抛出console.log()?如果len为0,那么你就不会得到任何输出