这里我编写代码来查找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(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);
}
});
}
答案 0 :(得分:1)
您应该添加从函数返回db.transaction
对象以及tx.executeSql
,
所以在函数调用之前添加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;
}
})
})
}
我假设您的查询代码运行时没有错误并获得预期的结果!
答案 1 :(得分:0)
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));