iamjonesy
iamjonesy

Reputation: 25122

My websql transaction won't execute

I have a mobile app in HTML5 that is using the websql database. I have a data.js file with a bunch of functions for doing various CRUD jobs with the database. I've never had this problem until I got to wiring this function. Basically the app is for creating quotes for tradesmen and the function I'm writing is getting the quote and quote lines, converting them into and array of JSON objects and ajaxing them to a web app.

For some reason my db.transaction's are not being executed. Can you help me figure out why? The db exists as other functions are calling this exact SQL. But it works for them and not this one:

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

}

I have both success and fail callbacks but neither is responding.

Also this is the first time I'm posting JSON from a JS app so feel free to comment.

Thanks,

Billy

Upvotes: 2

Views: 3123

Answers (2)

Adeyinka Adediji
Adeyinka Adediji

Reputation: 63

Copy this edited codes and see if it works

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

Upvotes: 1

XZVASFD
XZVASFD

Reputation: 1048

have you tried throwing console.log()'s in at the start of your success callbacks? if len is 0 in those, you wouldn't get any output from them as is

Upvotes: 0

Related Questions