简体   繁体   English

根据Phonegap SQLite中的先前查询结果进行查询

[英]Query based on previous query results in Phonegap SQLite

I am writing an Android app with Phonegap and its sqlite API. 我正在用Phonegap及其sqlite API编写一个Android应用程序。 Now I want to insert a record if some conditions are met in a previous query. 现在,如果以前的查询中满足某些条件,我想插入一条记录。 For example, 例如,

tx.executeSql("SELECT * FROM T_Task WHERE is_valid=1",[], successCB, errorCB);

In successCB, I want to add sub task if the task date is today and the sub task is not added for today yet. 在successCB中,如果任务日期是今天并且今天还没有添加子任务,我想添加子任务。 The condition to check if the task already exists is by task title. 检查任务是否已经存在的条件是按任务标题。

function successCB(tx,results){
 for(var i=0; i<results.rows.length; i++){
   var item=results.rows.item(i);
   if(item.date==today){//'today' is defined elsewhere, globally accessible
      var sql="SELECT * FROM T_SubTask WHERE title='"+item.title+"' and date='"+today+"')";
      tx.executeSql(sql,[],function(tx,results){addSubTask(tx, results, item.title)},errorCB);
   }
}

Now the problem is if there are multiple tasks, the passing parameter item.title will be overridden by the last loop. 现在的问题是,如果有多个任务,则传递参数item.title将被最后一个循环覆盖。 I know I can use something like (function(x){FUNCTION(x)})(item.title) to avoid similar issues. 我知道我可以使用类似(function(x){FUNCTION(x)})(item.title)之类的东西来避免类似的问题。 But I don't know how to do it in the sqlite case, where I want to pass the correct 'item.title' and 'results' parameters both. 但是我不知道如何在sqlite情况下做到这一点,我想同时传递正确的'item.title'和'results'参数。 If I use 如果我用

tx.executeSql(sql,[],function(tx,results){ (function(x){addSubTask(tx, results, x)})(item.title),errorCB);

, another problem is the results will be the input parameter of "successCB(tx, results)", not the results object for the "FROM T_SubTask" query. ,另一个问题是结果将是“ successCB(tx,results)”的输入参数,而不是“ FROM T_SubTask”查询的结果对象。

How can I make everything correct here? 我如何在这里使一切正确?

The problem with results can be easily fixed by changing the name of the argument to the anonymous success function in the "FROM T_SubTask" query. 通过将参数的名称更改为“ FROM T_SubTask”查询中的匿名成功函数,可以轻松解决results问题。

As for the item variable, you should be able move the code inside the if block into a separate function. 至于item变量,您应该可以将if块中的代码移动到单独的函数中。 Something like: 就像是:

function addSubtaskIfNeeded(item, tx) {
  var sql="SELECT * FROM T_SubTask WHERE title='"+item.title+"' and date='"+today+"')";
  tx.executeSql(sql,[],function(tx,subresults){addSubTask(tx, subresults, item.title)},errorCB);
}

Note, that the results variable is also renamed to subresults above. 需要注意的是, results变量也改名为subresults之上。 Then change the code inside the if block to call that function: 然后更改if块内的代码以调用该函数:

if (item.date == today) {
  addSubtaskIfNeeded(item, tx);
}

This way the item variable inside addSubtaskIfNeeded query callback function will be the one passed as argument to the function. 这样,在addSubtaskIfNeeded查询回调函数中的item变量将作为参数传递给该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM