IndexedDB - 讀取資料



我們將資料輸入到資料庫中,我們需要呼叫資料來檢視更改以及其他各種目的。

我們必須呼叫物件儲存上的 get() 方法來讀取該資料。get 方法採用了您想要從儲存中檢索的物件的主鍵。

語法

var request = objectstore.get(data);

在這裡,我們要求物件儲存使用 get() 函式獲取資料。

示例

以下示例是請求物件儲存獲取資料的一個執行 −

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Document</title>
</head>
<body>
   <script>
      const request = indexedDB.open("botdatabase",1);
      request.onupgradeneeded = function(){
         const db = request.result;
         const store = db.createObjectStore("bots",{ keyPath: "id"});
      }
      request.onsuccess = function(){
         document.write("database opened successfully");
         const db = request.result;
         const transaction=db.transaction("bots","readwrite");
         const store = transaction.objectStore("bots");
         store.add({id: 1, name: "jason",branch: "IT"});
         store.add({id: 2, name: "praneeth",branch: "CSE"});
         store.add({id: 3, name: "palli",branch: "EEE"});
         store.add({id: 4, name: "abdul",branch: "IT"});
         const idquery = store.get(4);
         idquery.onsuccess = function(){
            document.write("idquery",idquery.result);
         }
         transaction.oncomplete = function(){
            db.close;
         }
      }
   </script>
</body>
</html>

輸出

database opened successfully
idquery {id: 4, name: 'abdul', branch: 'IT'} 
廣告
© . All rights reserved.