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

发布于 2024-12-12 05:57:54 字数 144 浏览 0 评论 0原文

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

I run a SQL SELECT statement using executeAsync function of mozIStorageStatement object, the thing is, when the statement get no results, the handleresult function of the callback doesn't run. Is this a normal behaviour or do I have a bug? If it is a normal behaviour, then how do I write a code that will run in the case we have zero results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-12-19 05:57:54

这是正常行为。仅当结果可用时才会调用 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 该语句停止执行,因为
    发生错误。

进一步阅读:

This is normal behavior. The handleResult method is called only when results are available, which is never (when a query returns the empty set). You can handle this case in the handleCompletion method, which always executes, whether or not the query returns any rows.

Here's a throwaway example to demonstrate:

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);

The aReason parameter can have the following values:

  • REASON_FINISHED = 0 The statement has finished executing normally.
  • REASON_CANCELED = 1 The statement stopped executing because it was canceled.
  • REASON_ERROR = 2 The statement stopped executing because an
    error occurred.

Further reading:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文