in webOS, I need a method that, using the input, will doe a sql select command and returns the restults as an array

安稳与你 提交于 2019-12-11 07:49:22

问题


So the subject says it all, but here is more detail. I'd like to create a method that when passed a set of values, it inserts these values in a sql select statment and then returns the results. I'm working on a webOS app so the structure for calling the sqlite database is as such:

function getSQLresults(input){
    this.db.transaction(function(transaction) {
         transaction.executeSql(
             theSql,
             parmAry,
             function(transaction, results) {}, //this is the on success function
             function(transaction, error) {} //this is the on failure function
         );
    }.bind(this));
};

So, I'm comfortable with a simple method that is one function with or without input that has a return statement(s) in it. The problem I have here is that the results come into the onSuccess statment via the results variable. Since the results are buried in many nested functions, I have no idea how to make something like the following work:

var sqlResults = getSQLresults(varToSend);

Can someone explain how to make this work?


回答1:


Assuming you're not well experienced with JavaScript, you'll want to read up on asynchronous programming and callbacks as that is really the essence of JavaScript. When your getSQLresults function executes, it first fires off an asynchronous function with "this.db.transaction", which essentially asks the DB to establish a transaction and to execute a callback once it's established. The (function(transaction) { .... }) you see immediately following that is a new function that is being supplied to the db.transaction function as a callback in the form of a parameter. Once the db has established the transaction, it will execute this callback function.

When that happens, a new asynchronous function is called which is "transaction.executeSql", which is asking the transaction you previously established to perform a SQL query and return the results (or an error). You'll note that after the "theSql" and "parmAry" parameters, you're passing in two new functions. Both of these are also known as callbacks. They're functions passed into the "transaction.executeSql" function as parameters to be called back when the function has completed.

The key to all of this is that it happens asynchronously, not synchronously - meaning the program doesn't halt and wait for the response. The program will fire off these events and keep going ( and RETURNing from getSQLresults function ) before the other functions are done. As such, you cannot simply assign a value to the results of that function and expect to get a meaningful result. Instead, because you're dealing with asynchronous functions, you too will have to follow an asynchronous programming style. To do that, you simply need to pass in your OWN callback to be executed when your sql transactions are completed.......

function getSQLresults(input, callback){ // <---- new callback param
    this.db.transaction(function(transaction) {
         transaction.executeSql(
             theSql,
             parmAry,
             function(transaction, results) { 
                  // Execute the callback function, passing in our results
                  callback(results); 
             }, //this is the on success function
             function(transaction, error) {} //this is the on failure function
         );
    }.bind(this));
};

// Setup a callback function to pass INTO getSQLresults as a parameter.
function fnToExecuteForCallback(results) {
     // The results from the "transaction.executeSql" statement will now 
     // be available here, as they were passed in by executing this 
     // callback function - which is the "callback" parameter passed 
     // into getSQLresults...
}

var someInput = "???";

// Call the getSQLresults function, passing in our input and callback function.
getSQLresults(someInput, fnToExecuteForCallback);


来源:https://stackoverflow.com/questions/6260018/in-webos-i-need-a-method-that-using-the-input-will-doe-a-sql-select-command-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!