在 webOS 中,我需要一种方法,使用输入执行 sql select 命令并将结果作为数组返回

发布于 2024-11-14 04:48:37 字数 710 浏览 0 评论 0原文

所以主题说明了一切,但这里有更多细节。我想创建一个方法,当传递一组值时,它将这些值插入到 sql select 语句中,然后返回结果。我正在开发一个 webOS 应用程序,因此调用 sqlite 数据库的结构如下:

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));
};

因此,我对一种简单的方法感到满意,该方法是一个带或不带输入的函数,其中包含 return 语句。我这里遇到的问题是结果通过 results 变量进入 onSuccess 语句。由于结果被隐藏在许多嵌套函数中,我不知道如何进行如下工作:

var sqlResults = getSQLresults(varToSend);

有人可以解释如何进行这项工作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2024-11-21 04:48:37

假设您对 JavaScript 没有丰富的经验,您将需要阅读异步编程和回调,因为这确实是 JavaScript 的本质。当您的 getSQLresults 函数执行时,它首先使用“this.db.transaction”触发一个异步函数,该函数本质上要求数据库建立一个事务并在建立后执行回调。您紧随其后看到的 (function(transaction) { .... }) 是一个新函数,它以参数形式作为回调提供给 db.transaction 函数。一旦数据库建立了事务,它将执行此回调函数。

当发生这种情况时,将调用一个新的异步函数“transaction.executeSql”,它要求您之前建立的事务执行 SQL 查询并返回结果(或错误)。您会注意到,在“theSql”和“parmAry”参数之后,您传递了两个新函数。这两者也称为回调。它们是作为参数传递到“transaction.executeSql”函数中的函数,在函数完成时回调。

所有这一切的关键在于它是异步发生的,而不是同步发生的——这意味着程序不会停止并等待响应。在其他函数完成之前,程序将触发这些事件并继续运行(并从 getSQLresults 函数返回)。因此,您不能简单地为该函数的结果赋值并期望获得有意义的结果。相反,因为您正在处理异步函数,所以您也必须遵循异步编程风格。为此,您只需传入自己的回调,以便在 sql 事务完成时执行......

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文