通过ID返回sqlite中的true或fals



在这里,我编写了代码以查找sqlite表的行,以及如果可用的行,则返回false,否则为true。

在这里代码

function findPageID(pickId)
{   
    this.db.transaction(function(tx){
                tx.executeSql("SELECT pid FROM page WHERE pid="+pickId, [], function(tx, result) {
                    if(result) {                           
                        if(result.rows.length > 0){
                            return false;       
                        }
                        else
                        {
                            return true;
                        }                                                       
                    }
                })
            })
}

当我尝试使用console.log(findPageID(pickId));

它的表演undefined,任何人都可以找到我一个解决方案

在这里完整的代码

function udatefetchPagedata() {
$.ajax({
    url: syncPageURL,
    dataType: "json",
    success: function(data) {
        console.log("The server returned " + data.length + " changes that occurred after ");
        for (var ci in data) {
            alert(findPageID(data[ci].pid))
            //if(findPageID(data[ci].pid))
            //{
            //callback(data);                
            //insertPage(data[ci].pid, data[ci].ptitle, data[ci].pcontent, data[ci].page_slug, data[ci].porder, data[ci].pagepub, data[ci].mobicon, data[ci].mobborder, data.length);
            //setLastSyncPageDevice();
            //}
        }
    },
    error: function(model, response) {
        //alert(response.responseText);
    }
});
}

您应该添加返回 db.transaction对象以及函数中的 tx.executeSql,因此,在您的功能调用之前添加返回

function findPageID(pickId)
{   
    return this.db.transaction(function(tx){
                   return tx.executeSql("SELECT pid FROM page WHERE pid="+pickId, [], function(tx, result) {
                    if(result) {    
                        if(result.rows.length > 0){
                            return false;                                   
                        }
                        else
                        {
                            return true;
                        }    
                    }
                    else{
                        return false;  
                    }
                })
            })
}

我假设您的查询代码在没有错误的情况下运行并获得预期结果!

function findPageID(pickId) {
  return new Promise((resolve, reject) => {
    this.db.transaction(function(tx) {
      tx.executeSql('SELECT pid FROM page WHERE pid=' + pickId, [], function(tx, result) {
        if (result) {
          if (result.rows.length > 0) {
            resolve(false);
          } else {
            resolve(true);
          }
        }
      });
    });
  });
}

并将其称为findPageID(pickId).then(flag => console.log(flag));

,或者如果您喜欢回调

function findPageID(pickId, cb) {
  this.db.transaction(function(tx) {
  tx.executeSql('SELECT pid FROM page WHERE pid=' + pickId, [], function(tx, result) {
    if (result) {
        if (result.rows.length > 0) {
          return cb(false);
        } else {
          return cb(true);
        }
      }
    });
  });
}

并将其称为findPageID(pickId, (flag) => console.log(flag));