简体   繁体   中英

SQlite database with PhoneGap and JavaScript

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?

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*/
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM