<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Web前端入門第 85 問:JavaScript 一個簡單的 IndexedDB 數據庫入門示例

      在前端風風雨雨的混了多年,從沒在項目中實際使用過 IndexedDB 這個瀏覽器端的數據庫,所以今天就摸了下 MDN 的后門,寫一個簡單的入門示例。

      頁面大概長這樣:

      85-1

      源碼:

      以下代碼包含了一個數據庫所有的 CRUD (增刪改查)操作。

      <div>
        <button id="js_add_btn">添加書籍</button>
      </div>
      <div>
        <input type="text" name="" id="js_book_id" placeholder="輸入書籍ID"/>
        <button id="js_get_by_id_btn">查詢書籍</button>
        <button id="js_update_by_id_btn">更新書籍</button>
        <button id="js_delete_by_id_btn">刪除書籍</button>
      </div>
      <div>
        <input type="text" name="" id="js_book_author" placeholder="輸入書籍作者查詢"/><button id="js_get_by_author_btn">查詢書籍</button>
      </div>
      <div>
        <button id="js_get_all_btn">查詢所有書籍</button>
      </div>
      <div id="output"></div>
      
      <script>
        (() => {
          // 數據庫配置
          const dbName = 'MyDB';
          const storeName = 'books';
          const output = document.getElementById('output')
          let db = null;
      
          function setOutputContent(html) {
            output.innerText = html;
          }
      
          // 初始化數據庫
          const initDB = () => {
            const request = indexedDB.open(dbName);
      
            // 數據庫升級/創建時觸發
            request.onupgradeneeded = (event) => {
              db = event.target.result;
      
              // 創建對象存儲空間(類似數據庫表)
              const store = db.createObjectStore(storeName, {
                keyPath: 'id', // 主鍵
                autoIncrement: true, // 自動生成ID
              });
      
              // 創建索引(允許通過作者查詢)
              store.createIndex('author_idx', 'author', { unique: false });
      
              console.log('數據庫已創建/升級');
            };
      
            // 數據庫打開成功
            request.onsuccess = (event) => {
              db = event.target.result;
              console.log('數據庫已打開');
            };
      
            // 數據庫打開失敗
            request.onerror = (event) => {
              console.error('數據庫錯誤:', event.target.error);
              setOutputContent('數據庫錯誤');
            };
          };
      
          // 封裝包裝函數,用于執行數據庫相關方法
          function wrapper(func) {
            return new Promise((resolve, reject) => {
              const transaction = db.transaction([storeName], 'readwrite');
              const store = transaction.objectStore(storeName);
              const res = func(store)
              res.onsuccess = () => {
                resolve(res.result)
              };
      
              res.onerror = (event) => {
                reject(event.target.error)
              };
            });
          }
      
          // 添加數據
          const addBook = async (book) => {
            try {
              const result = await wrapper((store) => store.add(book))
              console.log(result);
              setOutputContent(`添加成功!書籍ID: ${result}`);
            } catch (error) {
              console.error(error);
              setOutputContent('添加失敗');
            }
          };
      
          // 通過ID查詢數據
          const getBook = async (id) => {
            try {
              const book = await wrapper((store) => store.get(id))
              if (book) {
                console.log('查詢結果:', book);
                setOutputContent(`書名: ${book.title}\n作者: ${book.author}\n價格: $${book.price}`);
              } else {
                console.log('未找到書籍');
                setOutputContent('未找到該書籍');
              }
            } catch (error) {
              console.error(error);
              setOutputContent('查詢失敗');
            }
          };
      
          // 查詢全部數據
          const getAllBook = async () => {
            try {
              const book = await wrapper((store) => store.getAll())
              if (book) {
                console.log('查詢結果:', book);
                setOutputContent(`總數:${book.length}`);
              } else {
                console.log('未找到書籍');
                setOutputContent('未找到該書籍');
              }
            } catch (error) {
              console.error(error);
              setOutputContent('查詢失敗');
            }
          };
      
          // 更新數據--數據不存在時會添加數據
          const updateBook = async (book) => {
            try {
              const result = await wrapper((store) => store.put(book))
              console.log(result);
              setOutputContent(`更新成功!結果: ${result}`);
            } catch (error) {
              console.error(error);
              setOutputContent('更新失敗');
            }
          };
      
          // 刪除數據
          const deleteBook = async (id) => {
            try {
              const result = await wrapper((store) => store.delete(id))
              console.log(result);
              setOutputContent(`刪除成功!結果: ${result}`);
            } catch (error) {
              console.error(error);
              setOutputContent('刪除失敗');
            }
          };
          // 根據作者查詢
          const getByAuthor = async (author) => {
            try {
              const result = await wrapper((store) => {
                const index = store.index("author_idx");
                const request = index.getAll(author);
                return request;
              })
              console.log(result);
              setOutputContent(`查詢成功!結果: ${JSON.stringify(result)}`);
            } catch (error) {
              console.error(error);
              setOutputContent('查詢失敗');
            }
          };
      
          // 添加書籍
          document.getElementById('js_add_btn').onclick = () => {
            addBook({
              title: 'Web前端入門',
              author: '前端路引',
              price: (0.99 * Math.random() + 10) .toFixed(2),
            });
          };
      
          // 根據ID查詢
          document.getElementById('js_get_by_id_btn').onclick = () => {
            const bookId = document.getElementById('js_book_id').value;
            getBook(Number(bookId));
          };
      
          // 根據ID刪除
          document.getElementById('js_delete_by_id_btn').onclick = () => {
            const bookId = document.getElementById('js_book_id').value;
            deleteBook(Number(bookId));
          };
      
          // 根據ID更新
          document.getElementById('js_update_by_id_btn').onclick = () => {
            const bookId = document.getElementById('js_book_id').value;
            updateBook({
              // 主鍵ID
              id: Number(bookId),
              title: 'Web前端入門',
              author: '公眾號:前端路引',
              price: (0.99 * Math.random() + 10) .toFixed(2),
            });
          };
      
          // 根據作者查詢
          document.getElementById('js_get_by_author_btn').onclick = () => {
            const author = document.getElementById('js_book_author').value
            getByAuthor(author);
          };
      
          // 查詢全部
          document.getElementById('js_get_all_btn').onclick = () => {
            getAllBook();
          };
      
          // 頁面加載時初始化數據庫
          initDB();
        })();
      </script>
      

      IndexedDB 大小限制

      以下內容為 AI 回答:

      85-2

      獲取可用的存儲空間大小

      navigator.storage.estimate().then((estimate) => {
        console.log(
          `已使用:`,
          (
            (estimate.usage / estimate.quota) * 100
          ).toFixed(2)
        );
        console.log(`可使用:`, (estimate.quota / 1024 / 1024).toFixed(2) + "MB");
      });
      

      相關文檔:https://developer.mozilla.org/en-US/docs/Web/API/Storage_API

      寫在最后

      由于項目中很少使用,所以這 API 給不了太多建議~~

      此 API 的應用場景還是有的,可以想想下那些超大文件在線處理應用,比如 ZIP、PSD、PPT 之類的文件,可以將文件解析后存在 IndexedDB 中,在需要的時候查詢指定數據即可,這樣可以節約很多的解析時間。

      只是需要注意,所有存在瀏覽器端的數據,用戶清空緩存之后都將不復存在,所以在使用時需做好容錯處理~~

      詳細文檔請參閱 MDN:https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API

      一個更加完善的項目:https://github.com/marco-c/eLibri

      posted @ 2025-08-28 10:27  前端路引  閱讀(319)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 加勒比无码人妻东京热| 元阳县| 精品人妻一区二区| 国产玖玖玖玖精品电影| 曰韩亚洲av人人夜夜澡人人爽| 国产精品中文字幕在线| 精品偷拍一区二区三区在| 精品国产成人国产在线观看| 久久香蕉国产线看观看猫咪av| 亚洲国产韩国欧美在线| 久久综合97丁香色香蕉| 黄色特级片一区二区三区| 亚洲理论电影在线观看| 无套内射极品少妇chinese| 精品国产午夜理论片不卡| 亚洲精品一区二区妖精| 一区二区三区激情都市| 久热综合在线亚洲精品| 波多野结衣av一区二区三区中文| 免费十八禁一区二区三区| 亚洲人成网站18禁止无码| 成人性能视频在线| 人人爽人人爽人人片av东京热| 97成人碰碰久久人人超级碰oo| 99精产国品一二三产品香蕉| 午夜色无码大片在线观看免费| 国产av无码专区亚洲av软件| 国产精品午夜福利在线观看| 中文字幕无码免费久久| 双乳奶水饱满少妇呻吟免费看| 亚洲av中文乱码一区二| 久久精品国产www456c0m| 日韩精品有码中文字幕| 欧美人成精品网站播放| 国产睡熟迷奷系列网站| 国产成人高清亚洲综合| 18禁国产一区二区三区| 日本精品人妻无码77777| 午夜福利国产盗摄久久性| 精品国产中文字幕在线| 強壮公弄得我次次高潮A片|