Ben Pearce
Ben Pearce

Reputation: 7094

db.transaction, what comes first the error callback or the success call back

I'm building a Phone Gap app and using the db.transaction function following examples in this documentation. I'm seeing some conflicting information on the order of parameter in the db.transaction function and I was hoping someone could explain the discrepancy I'm seeing.

In the example below the error call back is the second parameter and the success call back is the third parameter:

function populateDB(tx) {
     tx.executeSql('DROP TABLE IF EXISTS DEMO');
     tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
     tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
     tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

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

function successCB() {
    alert("success!");
}

var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);

Here's another example where the success callback is the third parameter and the error callback is the fourth.

function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }

    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }

    function querySuccess(tx, results) {
        var len = results.rows.length;
        console.log("DEMO table: " + len + " rows found.");
        for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
        }
    }

    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }

    function successCB() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(queryDB, errorCB);
    }

Upvotes: 1

Views: 3675

Answers (1)

MugosDynamic
MugosDynamic

Reputation: 99

No, there is no conflict here. 'transaction' is method of the 'Database' object and it's parameters like 'query,errorCallback,successCallback'.

On the other hand; 'executeSql' is method of the 'Transaction' object and it's parameters like 'query,IDONTKNOW,successCallback,errorCallback'.

So documentation says the truth but i didn't understand why sequence of callback parameters are different,too. I think different guys designed that objects.

Upvotes: 2

Related Questions