{"id":1971,"date":"2025-02-23T14:56:25","date_gmt":"2025-02-23T06:56:25","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1971"},"modified":"2025-02-23T14:56:25","modified_gmt":"2025-02-23T06:56:25","slug":"html5-web-indexeddb-%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/23\/html5-web-indexeddb-%e6%95%b0%e6%8d%ae%e5%ba%93\/","title":{"rendered":"HTML5 Web IndexedDB \u6570\u636e\u5e93"},"content":{"rendered":"\n<p><strong>HTML5 IndexedDB<\/strong> \u662f\u4e00\u4e2a\u4f4e\u7ea7\u522b\u7684 API\uff0c\u5141\u8bb8\u6d4f\u89c8\u5668\u4e2d\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u7684\u6570\u636e\u3002\u5b83\u662f Web \u5b58\u50a8\u7684\u4e00\u90e8\u5206\uff0c\u4e3a\u73b0\u4ee3 Web \u5e94\u7528\u63d0\u4f9b\u4e86\u6bd4 Web SQL \u66f4\u5f3a\u5927\u7684\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\uff0c\u80fd\u591f\u5b58\u50a8\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u6587\u4ef6\u6216\u4e8c\u8fdb\u5236\u6570\u636e\uff09\u3002\u4e0e Web SQL \u4e0d\u540c\uff0cIndexedDB \u4f7f\u7528\u5bf9\u8c61\u5b58\u50a8\u800c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4e14\u4e0d\u4f9d\u8d56 SQL \u8bed\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3b\u8981\u7279\u70b9\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b58\u50a8\u5927\u6570\u636e\u91cf\uff1a<\/strong> IndexedDB \u5141\u8bb8\u5b58\u50a8\u5927\u91cf\u6570\u636e\uff08\u901a\u5e38\u4e3a\u51e0 GB\uff09\uff0c\u6bd4 Web SQL \u5b58\u50a8\u5bb9\u91cf\u5927\u5f97\u591a\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u52a1\u652f\u6301\uff1a<\/strong> \u6570\u636e\u64cd\u4f5c\uff08\u5982\u8bfb\u53d6\u3001\u5199\u5165\u3001\u5220\u9664\uff09\u90fd\u5728\u4e8b\u52a1\u4e2d\u8fdb\u884c\uff0c\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u64cd\u4f5c\uff1a<\/strong> \u4e0e Web SQL \u4e0d\u540c\uff0cIndexedDB \u7684 API \u662f\u5f02\u6b65\u7684\uff0c\u80fd\u591f\u5904\u7406\u5927\u91cf\u6570\u636e\u800c\u4e0d\u963b\u585e\u6d4f\u89c8\u5668\u7684\u4e3b\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u5bf9\u8c61\u5b58\u50a8\uff1a<\/strong> \u6570\u636e\u662f\u4ee5\u5bf9\u8c61\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u952e\u503c\u5bf9\uff08\u4e0e\u4f20\u7edf\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e0d\u540c\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">IndexedDB \u7684\u57fa\u672c\u6d41\u7a0b\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6253\u5f00\u6570\u636e\u5e93<\/strong><\/li>\n\n\n\n<li><strong>\u521b\u5efa\u5bf9\u8c61\u5b58\u50a8\uff08Object Store\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u6267\u884c\u4e8b\u52a1<\/strong><\/li>\n\n\n\n<li><strong>\u6dfb\u52a0\u3001\u67e5\u8be2\u3001\u5220\u9664\u6570\u636e<\/strong><\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u7d22\u5f15\u63d0\u9ad8\u67e5\u8be2\u6548\u7387<\/strong><\/li>\n\n\n\n<li><strong>\u5173\u95ed\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">IndexedDB \u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>\u6253\u5f00\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p>\u4f7f\u7528 <code>indexedDB.open()<\/code> \u6253\u5f00\u6216\u521b\u5efa\u6570\u636e\u5e93\u3002\u82e5\u6570\u636e\u5e93\u5df2\u5b58\u5728\u5219\u6253\u5f00\uff0c\u5426\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let db;\nconst request = indexedDB.open('MyDatabase', 1);\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u65f6\u6267\u884c\nrequest.onupgradeneeded = function(event) {\n  db = event.target.result;\n  \/\/ \u521b\u5efa\u5bf9\u8c61\u5b58\u50a8\n  const objectStore = db.createObjectStore('users', { keyPath: 'id' });\n  objectStore.createIndex('name', 'name', { unique: false });\n  objectStore.createIndex('age', 'age', { unique: false });\n};\n\n\/\/ \u6253\u5f00\u6570\u636e\u5e93\u6210\u529f\u65f6\u6267\u884c\nrequest.onsuccess = function(event) {\n  db = event.target.result;\n  console.log(\"Database opened successfully!\");\n};\n\n\/\/ \u6253\u5f00\u6570\u636e\u5e93\u5931\u8d25\u65f6\u6267\u884c\nrequest.onerror = function(event) {\n  console.log(\"Database error: \" + event.target.errorCode);\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u521b\u5efa\u5bf9\u8c61\u5b58\u50a8\uff08Object Store\uff09<\/strong><\/h4>\n\n\n\n<p>\u5bf9\u8c61\u5b58\u50a8\u662f IndexedDB \u4e2d\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff0c\u7c7b\u4f3c\u4e8e\u8868\u683c\uff08\u4f46\u4e0d\u9700\u8981\u5b9a\u4e49\u5b57\u6bb5\uff09\u3002\u6bcf\u4e2a\u5bf9\u8c61\u5b58\u50a8\u90fd\u6709\u4e00\u4e2a <strong>\u4e3b\u952e<\/strong>\uff08Key\uff09\uff0c\u7528\u4e8e\u552f\u4e00\u6807\u8bc6\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u5728 <code>onupgradeneeded<\/code> \u4e8b\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f1a\u521b\u5efa\u5bf9\u8c61\u5b58\u50a8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const objectStore = db.createObjectStore('users', { keyPath: 'id' });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>keyPath<\/code>\uff1a\u6307\u5b9a\u5bf9\u8c61\u5b58\u50a8\u7684\u4e3b\u952e\uff08\u53ef\u4ee5\u662f\u5bf9\u8c61\u7684\u5c5e\u6027\uff09\u3002<\/li>\n\n\n\n<li>\u521b\u5efa\u7d22\u5f15\uff1a\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u7279\u5b9a\u7684\u5c5e\u6027\u521b\u5efa\u7d22\u5f15\uff0c\u4ee5\u4fbf\u8fdb\u884c\u9ad8\u6548\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>objectStore.createIndex('name', 'name', { unique: false });\nobjectStore.createIndex('age', 'age', { unique: false });<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u5728\u6570\u636e\u5e93\u7684\u4e8b\u52a1\u4e2d\u4f7f\u7528 <code>add()<\/code> \u6216 <code>put()<\/code> \u65b9\u6cd5\u6dfb\u52a0\u6570\u636e\u3002<code>put()<\/code> \u65b9\u6cd5\u7528\u4e8e\u66f4\u65b0\u5df2\u5b58\u5728\u7684\u8bb0\u5f55\uff0c\u800c <code>add()<\/code> \u65b9\u6cd5\u4f1a\u6dfb\u52a0\u65b0\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const transaction = db.transaction('users', 'readwrite');\nconst objectStore = transaction.objectStore('users');\n\nconst user = { id: 1, name: 'Alice', age: 25 };\nobjectStore.add(user);\n\nconst user2 = { id: 2, name: 'Bob', age: 30 };\nobjectStore.add(user2);\n\ntransaction.oncomplete = function() {\n  console.log(\"Data added successfully!\");\n};\n\ntransaction.onerror = function() {\n  console.log(\"Error adding data.\");\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h4>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>get()<\/code> \u6216 <code>getAll()<\/code> \u65b9\u6cd5\u67e5\u8be2\u6570\u636e\u3002<code>get()<\/code> \u65b9\u6cd5\u901a\u8fc7\u4e3b\u952e\u83b7\u53d6\u5355\u4e2a\u8bb0\u5f55\uff0c<code>getAll()<\/code> \u65b9\u6cd5\u5219\u8fd4\u56de\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const transaction = db.transaction('users', 'readonly');\nconst objectStore = transaction.objectStore('users');\n\n\/\/ \u6839\u636e ID \u83b7\u53d6\u6570\u636e\nconst request = objectStore.get(1);\nrequest.onsuccess = function() {\n  console.log(request.result); \/\/ \u8f93\u51fa\uff1a{id: 1, name: 'Alice', age: 25}\n};\n\n\/\/ \u83b7\u53d6\u6240\u6709\u6570\u636e\nconst requestAll = objectStore.getAll();\nrequestAll.onsuccess = function() {\n  console.log(requestAll.result); \/\/ \u8f93\u51fa\u6240\u6709\u7528\u6237\u7684\u6570\u636e\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. <strong>\u5220\u9664\u6570\u636e<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e3b\u952e\u5220\u9664\u6307\u5b9a\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const transaction = db.transaction('users', 'readwrite');\nconst objectStore = transaction.objectStore('users');\n\n\/\/ \u5220\u9664\u6307\u5b9a ID \u7684\u6570\u636e\nconst request = objectStore.delete(1);\n\nrequest.onsuccess = function() {\n  console.log(\"Data deleted successfully!\");\n};\n\nrequest.onerror = function() {\n  console.log(\"Error deleting data.\");\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. <strong>\u4f7f\u7528\u7d22\u5f15\u67e5\u8be2\u6570\u636e<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u5bf9\u8c61\u5b58\u50a8\u4e2d\u6709\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8fdb\u884c\u67e5\u8be2\u3002\u7d22\u5f15\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const transaction = db.transaction('users', 'readonly');\nconst objectStore = transaction.objectStore('users');\nconst index = objectStore.index('name');\n\n\/\/ \u4f7f\u7528\u7d22\u5f15\u67e5\u627e\u7279\u5b9a\u540d\u79f0\u7684\u7528\u6237\nconst request = index.get('Alice');\nrequest.onsuccess = function() {\n  console.log(request.result); \/\/ \u8f93\u51fa\uff1a{id: 1, name: 'Alice', age: 25}\n};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7. <strong>\u5173\u95ed\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<p>\u5f53\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u53ef\u4ee5\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.close();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5927\u6570\u636e\u91cf\u5b58\u50a8\uff1a<\/strong> IndexedDB \u652f\u6301\u5b58\u50a8\u6bd4 Web SQL \u548c localStorage \u66f4\u5927\u7684\u6570\u636e\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u64cd\u4f5c\uff1a<\/strong> \u975e\u963b\u585e\u7684\u5f02\u6b65 API \u53ef\u4ee5\u907f\u514d\u963b\u585e UI \u7ebf\u7a0b\uff0c\u5904\u7406\u5927\u6570\u636e\u65f6\u4e0d\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u4e8b\u52a1\uff1a<\/strong> \u652f\u6301\u4e8b\u52a1\uff0c\u786e\u4fdd\u591a\u4e2a\u64cd\u4f5c\u7684\u4e00\u81f4\u6027\u548c\u539f\u5b50\u6027\u3002<\/li>\n\n\n\n<li><strong>\u590d\u6742\u6570\u636e\u5b58\u50a8\uff1a<\/strong> \u53ef\u4ee5\u5b58\u50a8\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u4e8c\u8fdb\u5236\u6570\u636e\u7b49\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u952e\u503c\u5bf9\u3002<\/li>\n\n\n\n<li><strong>\u67e5\u8be2\u80fd\u529b\uff1a<\/strong> \u652f\u6301\u7d22\u5f15\uff0c\u53ef\u4ee5\u5bf9\u5b58\u50a8\u7684\u6570\u636e\u8fdb\u884c\u9ad8\u6548\u67e5\u8be2\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f3a\u70b9\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u590d\u6742\u6027\u8f83\u9ad8\uff1a<\/strong> \u76f8\u5bf9\u4e8e Web SQL\uff0cIndexedDB \u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u7406\u89e3\u4e8b\u52a1\u3001\u5bf9\u8c61\u5b58\u50a8\u3001\u7d22\u5f15\u7b49\u6982\u5ff5\u3002<\/li>\n\n\n\n<li><strong>\u6d4f\u89c8\u5668\u652f\u6301\u5dee\u5f02\uff1a<\/strong> \u867d\u7136 IndexedDB \u5f97\u5230\u4e86\u5e7f\u6cdb\u7684\u652f\u6301\uff0c\u4f46\u5728\u4e00\u4e9b\u65e7\u7248\u672c\u7684\u6d4f\u89c8\u5668\u4e2d\u4ecd\u7136\u53ef\u80fd\u5b58\u5728\u4e0d\u5b8c\u5168\u652f\u6301\u7684\u60c5\u51b5\u3002<\/li>\n\n\n\n<li><strong>\u540c\u6b65\u4e0e\u5f02\u6b65 API\uff1a<\/strong> IndexedDB \u7684\u5f02\u6b65 API \u53ef\u80fd\u4f1a\u8ba9\u521d\u5b66\u8005\u611f\u5230\u6709\u4e9b\u590d\u6742\uff08\u9700\u8981\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6216 Promise\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u79bb\u7ebf Web \u5e94\u7528\uff1a<\/strong> \u53ef\u4ee5\u5c06\u5e94\u7528\u7684\u72b6\u6001\u6216\u7528\u6237\u6570\u636e\u5b58\u50a8\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u4ee5\u4fbf\u79bb\u7ebf\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5927\u578b\u6570\u636e\u5b58\u50a8\uff1a<\/strong> \u9700\u8981\u5728\u5ba2\u6237\u7aef\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u6570\u636e\u7684 Web \u5e94\u7528\uff0c\u5982\u90ae\u4ef6\u5ba2\u6237\u7aef\u3001\u56fe\u7247\u7f16\u8f91\u5668\u3001\u5730\u56fe\u5e94\u7528\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u7f13\u5b58\u4e0e\u6301\u4e45\u5316\uff1a<\/strong> \u7528\u4e8e\u7f13\u5b58 API \u6570\u636e\u6216\u7528\u6237\u8bbe\u7f6e\uff0c\u4f7f\u5e94\u7528\u80fd\u591f\u5728\u4e0d\u8fde\u63a5\u7f51\u7edc\u7684\u60c5\u51b5\u4e0b\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p>IndexedDB \u662f\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u5927\u91cf\u3001\u7ed3\u6784\u5316\u6570\u636e\u7684 Web \u5e94\u7528\u3002\u867d\u7136\u5b83\u76f8\u5bf9\u4e8e Web SQL \u66f4\u590d\u6742\uff0c\u4f46\u5b83\u7684\u5f02\u6b65\u7279\u6027\u548c\u4e8b\u52a1\u652f\u6301\u4f7f\u5b83\u6210\u4e3a\u5f00\u53d1\u73b0\u4ee3 Web \u5e94\u7528\u65f6\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTML5 IndexedDB \u662f\u4e00\u4e2a\u4f4e\u7ea7\u522b\u7684 API\uff0c\u5141\u8bb8\u6d4f\u89c8\u5668\u4e2d\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u5927\u91cf\u7ed3\u6784\u5316\u7684\u6570\u636e\u3002\u5b83\u662f Web [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","category-html5"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1971"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1971\/revisions"}],"predecessor-version":[{"id":1972,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1971\/revisions\/1972"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}