0

I'm using PhoneGap and a SQLite Database.

I try something like this:

I have a function useSavedThing():

function useSavedThing() {

        alert("BEFORE getSavedThing()");    

    getSavedThing();

        alert("AFTER getSavedThing()");

}

and a function getSavedThing():

function getSavedThing(){

        alert("ONE");
        var db = window.openDatabase("Database", "1.0", "Database", 200000);
    db.transaction(populateDB, errorCB, successCB);
        alert("TWO");
    function populateDB(tx) {

        alert("THREE");
    }

    function errorCB(tx, err) {
        alert("Error processing SQL: " + err);
    }

    function successCB() {
        alert("FOUR");
        db.transaction(getData);
    }

    function getData(tx) {
                alert("FIVE");
        tx.executeSql('SELECT * FROM SETTINGS', [], getIt, errorCB);
    }
    function getIt(tx, results) {
                   alert("SIX");
            var savedthing = results.rows.item(0).data;


    }



}

The problem is that when the function getSavedThing() is called, only the first two alerts (alert("ONE"), alert("TWO)) issued but then the alert ("AFTER getSavedThing ()") called.

But I want that all expenses alert ("ONE"), alert ("TWO"), alert ("THREE") alert ("FOUR") alert ("FIVE"), alert ("SIX"), issued before the alert("AFTER getSavedThing()"); appers.

The order fo the alerts should be:

  1. alert("BEFORE getSavedThing()");
  2. alert("ONE");
  3. alert("TWO");
  4. alert("THREE");
  5. alert("FOUR");
  6. alert("FIVE");
  7. alert("SIX");
  8. alert("BEFORE getSavedThing()");

Can someone help me?

1
  • can you show us any output in firebug or similar? I assume the functions inside getSavedThing() should be outside of it. Commented Feb 1, 2013 at 11:40

2 Answers 2

1

Database operations are performed asynchronously. If you want alert("AFTER getSavedThing()") to be executed after the last database operation it needs to be called from the callback function getIt();

function useSavedThing() {
    alert("BEFORE getSavedThing()");
    var afterGet = function(){
        alert("AFTER getSavedThing()");
    }
    getSavedThing( afterGet ); /*pass the callback function to getSavedThing*/
}

function getSavedThing( callback ){

    alert("ONE");
    var db = window.openDatabase("Database", "1.0", "Database", 200000);
    db.transaction(populateDB, errorCB, successCB);
    alert("TWO");
    function populateDB(tx) {
        alert("THREE");
    }

    function errorCB(tx, err) {
        alert("Error processing SQL: " + err);
    }

    function successCB() {
        alert("FOUR");
        db.transaction(getData);
    }

    function getData(tx) {
        alert("FIVE");
        tx.executeSql('SELECT * FROM SETTINGS', [], getIt, errorCB);
    }

    function getIt(tx, results) {
         alert("SIX");
         var savedthing = results.rows.item(0).data;
         callback.call(); /*execute the callback function*/
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats it! This is why u use stackoverflow :-) Love it! Thank very much!
0

You can use this framework for data access in PhoneGap, you will find it quite easy to use:

var db = new MyDB();
//query all users
var users = db.Users.toArrary(callback);

Comments

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.