Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass arguments to PhoneGap database transaction function

I have a working PhoneGap database transaction where I am able to run a sql query and process the results. However, in an effort to make it reusable, I need to be abe to pass arguments to the querying function. There should a better way than declaring global variables and accessing them/resetting in the query function. Appreciate any help in converting this:

    //update images function
    function updateGalleryCovers() {
        var db = window.openDatabase("test db", "1.0", "Cordova DB", 200000);
        db.transaction(queryDB_u_g, errorCB);
    }
    //Query the database
    function queryDB_u_g(tx) {
        var query = 'SELECT cover_img, objectId FROM USER_GALLERY WHERE userId="'+getUserId()+'"';
        tx.executeSql(query, [], querySuccess_u_g, errorCB);
    }
//Query success callback
function querySuccess_u_g(tx, results) {
    var len = results.rows.length;
    for (var i=0; i<len; i++){
        // process results
    }
}

to something like this:

    //update images function
    function updateGalleryCovers(userid) {
        var db = window.openDatabase("test db", "1.0", "Cordova DB", 200000);
        db.transaction(queryDB_u_g, userid, errorCB);
    }
    //Query the database
    function queryDB_u_g(tx, userid) {
        var query = 'SELECT cover_img, objectId FROM USER_GALLERY WHERE userId="'+userid+'"';
        tx.executeSql(query, [], querySuccess_u_g, errorCB);
    }
//Query success callback
function querySuccess_u_g(tx, results) {
    var len = results.rows.length;
    for (var i=0; i<len; i++){
        // process results
    }
}

Thanks!

like image 954
nikhilw Avatar asked Jan 16 '13 21:01

nikhilw


1 Answers

The transaction functions are offered by sqlite and not phonegap. Its true that you can't pass extra variables to the functions because of the method signature sqlite accepts.

But here's a work around for the same:

db_conn.transaction( function(tx){ your_function(tx, parameter1, parameter2) }, ErrorCallBack );

Here you are passing a dummy function to the transaction success callback and taking the transaction object along with it.

Hope that helps

like image 95
SHANK Avatar answered Oct 08 '22 21:10

SHANK