2

I am developing a mobile app. I use ajax calls to receive data from a webserver with this code:

$.ajax({
    url: 'http://www.xxxxxxxxxxxxxxxx',
    data: {
        name: 'Chad'
    },
    dataType: 'jsonp',
    success: function(data) {
        $.each(data.posts, function(i, post) {
            $.mobile.notesdb.transaction(function(t) {
                t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);',
                    [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno],
                    //$.mobile.changePage('#page3', 'slide', false, true),  
                    null);
            });
        });
    },
    complete: function() {
        test = 1;
    }
});

I want the complete function to be executed after all data inserted to SQLite...

How can I do this?

1
  • You need to pick up an event from sqlite, not from your AJAX call. Commented Feb 22, 2012 at 19:19

1 Answer 1

2

The complete callback should be in your executeSql function. executeSql should accept three arguments:

var sqlString = 'INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);';
var sqlValues = [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno];
var callback = function(){ test = 1; }
executeSql(sqlString, sqlValues, callback);

You should edit your executeSql function and make it accept a third argument, and end the function with callback();.

On another note, isn't passing SQL queries like this a security issue? Like, a major one?

Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to execute callback once??? because the callback function executed for each sqlite insert
I don't see how, since your SQL is executed asynchronously. You could count how many callbacks are returned, and execute a function when the numbers of callbacks returned equals the number of requests sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.