【问题标题】:My websql transaction won't execute我的 websql 事务不会执行
【发布时间】:2011-08-11 12:17:33
【问题描述】:

我有一个使用 websql 数据库的 HTML5 移动应用程序。我有一个 data.js 文件,其中包含用于使用数据库执行各种 CRUD 作业的函数。在我连接这个功能之前,我从来没有遇到过这个问题。基本上,该应用程序用于为商人创建报价,我正在编写的功能是获取报价和报价行,将它们转换为 JSON 对象数组并将它们 ajax 到 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,欢迎随时发表评论。

谢谢,

比利

【问题讨论】:

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


    【解决方案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'
        });
    }
    

    【讨论】:

      【解决方案2】:

      您是否尝试过在成功回调开始时抛出 console.log()?如果其中的 len 为 0,您将不会从它们那里得到任何输出

      【讨论】:

      • 我有,事情既不是成功也不是错误回调console.log被触发
      猜你喜欢
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多