在 webOS 中,我需要一种方法,该方法使用输入执行 sql select 命令并将结果作为数组返回 [英] in webOS, I need a method that, using the input, will doe a sql select command and returns the restults as an array

查看:22
本文介绍了在 webOS 中,我需要一种方法,该方法使用输入执行 sql select 命令并将结果作为数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以,我对一种简单的方法感到满意,该方法是一个带有或不带有返回语句的输入的函数.我在这里遇到的问题是结果通过 results 变量进入 onSuccess 语句.由于结果隐藏在许多嵌套函数中,我不知道如何使类似以下的工作:

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?

推荐答案

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

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.

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

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.

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

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

这篇关于在 webOS 中,我需要一种方法,该方法使用输入执行 sql select 命令并将结果作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆