SQLite additional parameter in callback

315 views
Skip to first unread message

asu

unread,
Feb 10, 2016, 1:34:15 AM2/10/16
to DroidScript
Hi,

The SQLite select calls a callback function with all the resulting database records.

I would like to provide a parameter from the calling SQlite select command to the callback function.

eg. 
        
{
var myParameter = 1;

db.ExecuteSql( "select * from tr_table where trDate = '"+_trDate+"' ;", [], myQueryCallback( myParameter), onError ); 
....
..
}

function myQueryCallback ( results, myParamter)
{
....
    do someting with myParamter  ......


Thanks
Asu


Dave Smart

unread,
Feb 10, 2016, 1:43:15 PM2/10/16
to androi...@googlegroups.com
Hi,

DroidScript does not encourage the use of anonymous functions because it leads to messy and unreadable code (which is difficult for beginners to understand), but if you are desperate to do that then here is the source code to the ExecuteSQL method which you can modify to allow anonymous callbacks. It might be useful to see this code anyway because it shows you how to use transactions too:-

             db.ExecuteSql = function( sql, params, success, error ) 
{
if( !success || !success.name ) success = null;
if( !error || !error.name ) error = _Err;
     
db.transaction( function(tx) { 
tx.executeSql( sql, params, 
function(tx,res) { if(success) success.apply(db,[res]) }, 
function(t,e) { error.apply(db,[e.message]); } 
); }, error
);
}



Here is the source to the full wrapper object which you could modify for you needs as required (just paste this code into your app and rename the 'this.' at the front of this method  to 'app.' and it will override the original :-

this.OpenDatabase = function( name ) 
{
_LoadScriptSync( "/Sys/cp.js" );
_LoadScriptSync( "/Sys/sql.js" );
_CreateCP( "sqliteplugin" );
var db = sqlitePlugin.openDatabase( name );
db.name = name;
   
   db.GetType = function() { return "Database"; }
   db.GetName = function() { return db.name; }
db.ExecuteSql = function( sql, params, success, error ) 
{
if( !success || !success.name ) success = null;
if( !error || !error.name ) error = _Err;
     
db.transaction( function(tx) { 
tx.executeSql( sql, params, 
function(tx,res) { if(success) success.apply(db,[res]) }, 
function(t,e) { error.apply(db,[e.message]); } 
); }, error
);
}
db.Close = function() { db.close( _Log, _Err ); }
db.Delete = function() { sqlitePlugin.deleteDatabase(db.name,_Log,_Err); }
return db;
}




asu

unread,
Feb 11, 2016, 6:33:06 AM2/11/16
to DroidScript
Hi Dave!

thanks a lot! I will look into this.

Problem lies in the asyncronity of Execute SQL  response. Often one needs to know what the basis (i.e. variables values/states) were when the query was startet. Variables might have been changed during SQL execute.

Thats the reason I was asking to provide additional parameters  in the callback.

Thanks again!
Asu

Dave Smart

unread,
Feb 11, 2016, 9:27:06 AM2/11/16
to DroidScript
I guess we could add a parameter to the ExecuteSQL method called 'extra' which gets carried through to the result callback.  Then you could add anything you liked :)

Steve Garman

unread,
Feb 11, 2016, 10:27:21 AM2/11/16
to DroidScript
Dave,
that would certainly be useful.

I agree with Asu that it sometimes involves jumping through hoops to avoid losing track of an individual query.

Reply all
Reply to author
Forward
0 new messages