在 webOS 中,我需要一种方法,使用输入执行 sql select 命令并将结果作为数组返回
所以主题说明了一切,但这里有更多细节。我想创建一个方法,当传递一组值时,它将这些值插入到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您对 JavaScript 没有丰富的经验,您将需要阅读异步编程和回调,因为这确实是 JavaScript 的本质。当您的 getSQLresults 函数执行时,它首先使用“this.db.transaction”触发一个异步函数,该函数本质上要求数据库建立一个事务并在建立后执行回调。您紧随其后看到的 (function(transaction) { .... }) 是一个新函数,它以参数形式作为回调提供给 db.transaction 函数。一旦数据库建立了事务,它将执行此回调函数。
当发生这种情况时,将调用一个新的异步函数“transaction.executeSql”,它要求您之前建立的事务执行 SQL 查询并返回结果(或错误)。您会注意到,在“theSql”和“parmAry”参数之后,您传递了两个新函数。这两者也称为回调。它们是作为参数传递到“transaction.executeSql”函数中的函数,在函数完成时回调。
所有这一切的关键在于它是异步发生的,而不是同步发生的——这意味着程序不会停止并等待响应。在其他函数完成之前,程序将触发这些事件并继续运行(并从 getSQLresults 函数返回)。因此,您不能简单地为该函数的结果赋值并期望获得有意义的结果。相反,因为您正在处理异步函数,所以您也必须遵循异步编程风格。为此,您只需传入自己的回调,以便在 sql 事务完成时执行......
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.......