我有一個已經存在的數據庫。我試圖從數據庫使用索引數據檢索數據,但我無法從數據庫中獲取數據。如何使用索引數據庫檢索數據庫中的數據
var data = [];
// creating or opening the database
var db;
var request = window.indexedDB.open("database");
request.onerror = function(event) {
console.log("error: ");
};
request.onsuccess = function(event) {
db = request.result;
console.log("success: "+ db);
};
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("Subject", {keyPath: "id"});
for (var i in data) {
objectStore.add(data[i]);
}
}
function readAll() {
var objectStore = db.transaction("Subject").objectStore("Subject");
console.log(objectStore);
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
alert("Name for id " + cursor.key + " is " + cursor.value.Subject);
cursor.continue();
}
else {
alert("No more entries!");
}
};
}
在此先感謝。
其實我試圖從數據庫獲取數據,但沒有顯示簡單的空白頁。 – Lucky
您確定IDB數據庫中實際存在數據嗎? – buley
是的,我的數據庫包含列Key,Subject,Category等的數據... – Lucky