I'm creating a script to read an attribute from a database - due to the architecture of the project it's easiest if this is implemented in JS.
I'm using IndexedDB, and I can find the database successfully, but when I try to get an attribute from the objectStore I'm getting an error (It seems I'm not able to access the 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: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
My code:
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");
}
The error occurs on the line var transaction = db.transaction(["TABLE"], "readonly");
.
The TABLE does exist - when the database is opened in a viewer the attribute is there.
I have not used .onupgradeneeded
since I am not creating any new objectstores - rather just trying to read one.
You seem to be trying to use IndexedDB to read from an existing SQLite file. That's not going to work. IndexedDB is self-contained within the browser, it doesn't have anything to do with external files. The only way to create object stores is to do it in onupgradendeeded
, and then you will also have to write the code to load data into them.
If this is a Cordova app, maybe this or this is more what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With