Javascript 在sqlite“按id查找”中返回true或false
在这里,我编写了查找SQLite表的行的代码,如果行的id可用,则返回false,否则返回true 这是它的代码Javascript 在sqlite“按id查找”中返回true或false,javascript,sqlite,Javascript,Sqlite,在这里,我编写了查找SQLite表的行的代码,如果行的id可用,则返回false,否则返回true 这是它的代码 function findPageID(pickId) { this.db.transaction(function(tx){ tx.executeSql("SELECT pid FROM page WHERE pid="+pickId, [], function(tx, result) { if(
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))代码>
它的显示未定义
,有人能帮我找到解决方案吗
这里是完整的代码
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);
}
});
}
您应该添加returndb.transaction
对象以及tx.executeSql
from函数,
因此,在函数调用之前添加return
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)那样调用它代码>
或者如果你愿意的话
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))那样调用它代码>它返回未定义您是否检查了SQL语句是否正确运行并返回了预期结果?它返回的null仍然是未定义的