使用mozIStorageStatement对象的executeAsync在SELECT中执行零结果行为

zero results behaviour in SELECT using executeAsync of mozIStorageStatement object,

本文关键字:执行 结果 SELECT 对象 mozIStorageStatement executeAsync 使用      更新时间:2023-09-26

我使用mozIStorageStatement对象的executeAsync函数运行SQL SELECT语句,问题是,当语句没有得到结果时,回调的handleresult函数不运行。这是正常的行为还是我有问题?如果这是一个正常的行为,那么我如何写一个代码,将运行在我们有零结果的情况下?

这是正常行为。只有当结果可用时才调用handleResult方法,而这种情况永远不会发生(当查询返回空集时)。您可以在handleCompletion方法中处理这种情况,无论查询是否返回任何行,该方法始终执行。

这里有一个简单的例子来演示:

Components.utils.import("resource://gre/modules/Services.jsm");  
Components.utils.import("resource://gre/modules/FileUtils.jsm");  
var DBTest = {
    // chrome window loaded
    onload: function(e) {
        var appContent = document.getElementById("appcontent");
        appContent.addEventListener("DOMContentLoaded", function(e) {
            try {
                var file = FileUtils.getFile("ProfD", ["cookies.sqlite"]);  
                var dbConn = Services.storage.openDatabase(file);
                var stmt = dbConn.createStatement(
                        "SELECT * FROM moz_cookies WHERE name='trash'");
                stmt.executeAsync({  
                    handleResult: function(aResultSet) {  
                        alert("handleResult");
                    },  
                    handleError: function(aError) {  
                        alert("handleError: " + aError.message);  
                    },      
                    handleCompletion: function(aReason) {  
                        alert("handleCompletion: " + aReason);
                    }  
                });  
            } catch(e) {
                alert(e);
            }
        }, true);
    }
}
window.addEventListener("load", DBTest.onload, false);

"aReason"参数取值如下:

  • REASON_FINISHED = 0语句正常执行完毕。
  • REASON_CANCELED = 1语句因为被取消而停止执行。
  • REASON_ERROR = 2语句停止执行错误发生。

进一步阅读:

  • https://developer.mozilla.org/En/MozIStorageStatementCallback