【问题标题】:Mozilla storageService: "Cannot execute statement :Error: Permission denied "Mozilla storageService:“无法执行语句:错误:权限被拒绝”
【发布时间】:2011-07-17 10:33:33
【问题描述】:

(在 Mozilla 上交叉发布:http://forums.mozillazine.org/viewtopic.php?f=7&t=2254955

我正在尝试使用Mozilla storageService。在以下代码中,要求用户授予创建本地数据库 ('database.sqlite') 的权限,并创建一个表。

<html>
<head>
<script type="text/javascript">
var con=null;
function executeStatement()
    {
    try
        {
        var stmt=con.createStatement("SELECT * FROM instances");
        }
    catch(e)
        {
        alert("Cannot execute statement :"+e);
        }
    }

function init()
    {
    try
        {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        }
    catch (e)
        {
        alert("Permission to write to file was denied.");
        return;
        }

    try
        {
        var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("Home", Components.interfaces.nsIFile);
        file.append("database.sqlite");

        var storageService = Components.classes["@mozilla.org/storage/service;1"]
                        .getService(Components.interfaces.mozIStorageService);
        con = storageService.openDatabase(file);
        con.executeSimpleSQL(
            "CREATE TABLE IF NOT EXISTS instances("+
                "id INTEGER PRIMARY KEY AUTOINCREMENT"+
            ")"
            );
        }
    catch(e)
        {
        alert(e);
        }

    }


window.addEventListener("load", init,true); 
</script>
</head>
<body>
<button onClick="executeStatement();">Test</button>
</body>
</html>

但是当我点击按钮调用方法executeStatement时,我得到以下异常:

无法执行语句:错误: 的权限被拒绝 调用方法UnnamedClass.createStatement

为什么?

【问题讨论】:

    标签: javascript sqlite permissions storage mozilla


    【解决方案1】:

    enablePrivilege 就是这样工作的——你必须在每个需要权限的函数中调用它。另见The only option is to include that block of code into each of my functions?

    在我们讨论的时候,我将引用该线程中 bz 的评论:

    我强烈建议您考虑完全不使用 enablePrivilege。它已被弃用并正在被删除。 – Boris Zbarsky 7 月 13 日 14:10

    ...

    替换使用的是扩展程序。我不确定这是多么清楚地记录了这一点,但即将推出的 Firefox 版本会在您使用 enablePrivilege 时发出警告,并且您不能再使用 enablePrivilege 来执行跨站点 XMLHttpRequest。在某些地方(例如 https://developer.mozilla.org/en/Mochitest#How_can_I_get_around_the_error_.22Permission_denied_to_get_property_XPCComponents.classes.22.3f )提到了这一点,但还有更多文档需要更新。 – Boris Zbarsky 7 月 14 日 5:11

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      相关资源
      最近更新 更多