如何将参数传递给PhoneGap数据库事务函数

时间:2022-12-07 23:08:01

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:

我有一个工作的PhoneGap数据库事务,在这里我可以运行sql查询并处理结果。然而,为了使其可重用,我需要abe将参数传递给查询函数。应该有比在查询函数中声明全局变量和访问它们/重新设置更好的方法。感谢您的帮助。

    //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!

谢谢!

1 个解决方案

#1


13  

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.

事务函数由sqlite提供,而不是phonegap。确实,您不能将额外的变量传递给函数,因为方法签名sqlite接受。

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

希望这有助于

#1


13  

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.

事务函数由sqlite提供,而不是phonegap。确实,您不能将额外的变量传递给函数,因为方法签名sqlite接受。

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

希望这有助于