索引数据库 "The operation failed because the requested database object could not be found. "

IndexedDB "The operation failed because the requested database object could not be found. "

我正在创建一个脚本来从数据库中读取属性 - 由于项目的体系结构,如果在 JS 中实现它是最简单的。

我正在使用 IndexedDB,我可以成功找到数据库,但是当我尝试从 objectStore 获取属性时出现错误(似乎我无法访问 Table):

Firefox: "NotFoundError: The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened."

Chrome: NotFoundError: 无法在 'IDBDatabase' 上执行 'transaction': 找不到指定的对象存储之一。

我的代码:

var request = window.indexedDB.open("/app_name/database.sqlite", 1);
request.onsuccess = function(event) {
    console.log("Found Database");
    db = event.target.result;

    var transaction = db.transaction(["TABLE"], "readonly");
    var objectStore = transaction.objectStore("TABLE");
    var ob = objectStore.get(String(ATTRIBUTE));

    ob.onsuccess = function(e) {
        console.log("Got ATTRIBUTE")
    }
    ob.onerror = function(e) {
        console.log("Have not got ATTRIBUTE")
    }

}
request.onerror = function(event) {
    console.log("Database not found");
}

错误发生在行var transaction = db.transaction(["TABLE"], "readonly");

TABLE 确实存在 - 当在查看器中打开数据库时,属性就在那里。

我没有使用 .onupgradeneeded 因为我没有创建任何新的对象存储 - 而只是尝试读取一个。

您似乎在尝试使用 IndexedDB 从现有的 SQLite 文件中读取数据。那是行不通的。 IndexedDB 在浏览器中是独立的,它与外部文件没有任何关系。创建对象存储的唯一方法是在 onupgradendeeded 中进行,然后您还必须编写代码以将数据加载到其中。

如果这是一个 Cordova 应用程序,也许 this or this 更符合您的要求。