从sqlite事务结果返回true或false

时间:2014-11-10 12:59:21

标签: javascript sqlite cordova

我正在使用sqlite和javascript for phoneGap。我正在尝试getMatchEvent(...)'函数的返回值(true或false)并通过检查addValueToDB(..)函数中的结果来执行某些操作。但是lookMatchEvent(...)函数没有立即返回值。现在它只是默认返回false值。 sql语句绝对没问题,我认为这是回调值的问题。如果任何人都可以在编码下面解决这个问题,那将会很棒。:)非常感谢提前...

function AddValueToDB(sportName, location, dateTime, team, teamOpp) {
if(lookMatchEvent(sportName, location, dateTime, team, teamOpp)) {//adding new row to database}
else {donot add data}
}

function lookMatchEvent($sportName, $location, $dateTime, $team, $teamOpp) {
    db.transaction(function(transaction) {
        transaction.executeSql("SELECT * from sport where sportName='"+$sportName+"' and location='"+$location+"' and dateTime='"+$dateTime+"' and team='"+$team+"' and teamOpp='"+$teamOpp+"';",
        [],
         function(transaction, result) {
            if (result != null && result.rows != null)
            {
                if (result.rows.length > 0) {
                    return false;
                }
                return false;
            }
            else
            {
                return true;
            }
        },errorHandler);
    },errorHandler,nullHandler);
}

2 个答案:

答案 0 :(得分:0)

回调函数以异步方式运行,return仅从 函数返回。

您必须将之后应该运行的代码拆分为单独的函数,然后从回调中调用它:

function AddValueToDB(sportName, location, dateTime, team, teamOpp) {
    lookMatchEvent(sportName, location, dateTime, team, teamOpp);
}

function AddValueToDB_part2(result) {
    if (result) {
        ...
    } else {
        ...
    }
}

function lookMatchEvent(...) {
    db.transaction(...
        ...
        function(transaction, result) {
            if (result != null && result.rows != null)
            {
                if (result.rows.length > 0) {
                    AddValueToDB_part2(false);
                }
                AddValueToDB_part2(false);
            }
            else
            {
                AddValueToDB_part2(true);
            }
        }
        ...
    );
}

如果lookMatchEvent函数不知道它的调用位置(因为它是从多个地方调用的),你可以将第二个函数作为参数:

function AddValueToDB(sportName, location, dateTime, team, teamOpp) {
    lookMatchEvent(sportName, location, dateTime, team, teamOpp,
                   function(result) {
        if (result) {
            ...
        } else {
            ...
        }
    });
}

function lookMatchEvent(..., my_callback) {
    db.transaction(...
        ...
        function(transaction, result) {
            if (result != null && result.rows != null)
            {
                if (result.rows.length > 0) {
                    my_callback(false);
                }
                my_callback(false);
            }
            else
            {
                my_callback(true);
            }
        }
        ...
    );
}

答案 1 :(得分:0)

你的回调就是问题。

function AddValueToDB(sportName, location, dateTime, team, teamOpp) {
    function doActionOnResult(transaction, result){
        //Do your action here
        if (result != null && result.rows != null)
        {
            if (result.rows.length > 0) {
                return false;
            }
            return false;
        }
        else
        {
            return true;
        }
    }

    lookMatchEvent(sportName, location, dateTime, team, teamOpp, doActionOnResult)
}

function lookMatchEvent($sportName, $location, $dateTime, $team, $teamOpp, callback) {
    db.transaction(function(transaction) {
        transaction.executeSql("SELECT * from sport where sportName='"+$sportName+"' and location='"+$location+"' and dateTime='"+$dateTime+"' and team='"+$team+"' and teamOpp='"+$teamOpp+"';",
        [],
        doActionOnResult,
        errorHandler);
    },errorHandler,nullHandler);
}