为什么在包含SQLite命令的函数中以错误的顺序处理语句?

时间:2014-03-11 23:09:12

标签: javascript sqlite

我是SQLite的新手(对于一般的编程来说相对较新),如果答案显而易见,那就很抱歉。有谁知道为什么当我运行以下功能时,最后的警报会显示在数据库事务内的警报之前?

“内部事务”警报显示变量“recipeRating”保存适当的数字(在这种情况下为10),但“外部事务”警报显示它保持为零。我想要的只是“recipeRating”变量在函数末尾保存正确的值,以便可以返回并在别处使用。任何人都可以告诉我如何实现这一点和/或警报以错误的顺序出现的原因?

function calculateRecipeRating(newRecipeName,newIngredient1,newIngredient2,newIngredient3){

var recipeRating = 0;

db.transaction(function(transaction) {
    transaction.executeSql('SELECT * FROM Ingredients', [], function(transaction, result) {
        if (result != null && result.rows != null) {
            for (var i = 0; i < result.rows.length; i++) {
                var row = result.rows.item(i);

                var ingRating = parseInt(row.IngredientRating);

                if(row.IngredientName == newIngredient1 || row.IngredientName == newIngredient2 || row.IngredientName == newIngredient3){

                    recipeRating = recipeRating + ingRating

                }

            }
        }

        alert("inside transaction = " + recipeRating);

    },errorHandler);
},errorHandler,nullHandler);

alert("after transaction = " + recipeRating);

}

0 个答案:

没有答案