<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>indexedDB(瀏覽器本地存儲(chǔ)數(shù)據(jù)庫)</title> </head> <body> <p>IndexedDB 就是瀏覽器提供的本地?cái)?shù)據(jù)庫,它可以被網(wǎng)頁腳本創(chuàng)建和操作。</p> <p>IndexedDB 允許儲(chǔ)存大量數(shù)據(jù),提供查找接口,還能建立索引。</p> <h3>indexedDB特點(diǎn)</h3> <ol> <li>鍵值對存儲(chǔ):采用對象倉庫存儲(chǔ)數(shù)據(jù),所有的數(shù)據(jù)類型都可以直接存入,主鍵是獨(dú)一無二的</li> <li>異步:</li> <li>支持事務(wù):IndexedDB 支持事務(wù)(transaction),這意味著一系列操作步驟之中,只要有一步失敗,整個(gè)事務(wù)就都取消,數(shù)據(jù)庫回滾到事務(wù)發(fā)生之前的狀態(tài),不存在只改寫一部分?jǐn)?shù)據(jù)的情況。</li> <li>同源限制: IndexedDB 受到同源限制,每一個(gè)數(shù)據(jù)庫對應(yīng)創(chuàng)建它的域名。網(wǎng)頁只能訪問自身域名下的數(shù)據(jù)庫,而不能訪問跨域的數(shù)據(jù)庫。</li> </ol> <script> /** * databaseName:字符串,表示數(shù)據(jù)庫的名字,不存在則新建 * version :第二個(gè)參數(shù)是整數(shù),表示數(shù)據(jù)庫的版本。默認(rèn)為1 * 返回一個(gè) IDBRequest 對象 對象通過三種事件error、success、upgradeneeded * * 新建數(shù)據(jù)庫與打開數(shù)據(jù)庫是同一個(gè)操作。如果指定的數(shù)據(jù)庫不存在,就會(huì)新建。不同之處在于,后續(xù)的操作主要在upgradeneeded事件的監(jiān)聽函數(shù)里面完成 * * 新建數(shù)據(jù)步驟: * step1:新建對象倉庫(即新建表 * step2:新建索引 * * */ //IDBDatabase對象 let db; //創(chuàng)建或者打開 let request = window.indexedDB.open('newIDB'); //刪除數(shù)據(jù)庫 var DBDeleteRequest = window.indexedDB.deleteDatabase('newIDB'); DBDeleteRequest.onerror = function (event) { console.log('Error'); }; DBDeleteRequest.onsuccess = function (event) { console.log('success'); }; //error事件--表示打開數(shù)據(jù)失敗 request.onerror = function (event) { console.log('數(shù)據(jù)庫打開報(bào)錯(cuò)'); db.close(); }; request.success = function (event) { //通過request對象的result屬性拿到數(shù)據(jù)庫對象 db = request.result; console.log(db); console.log('數(shù)據(jù)庫打開成功') }; console.log(request); //onupgradeneeded---如果指定的版本號,大于數(shù)據(jù)庫的實(shí)際版本號,就會(huì)發(fā)生數(shù)據(jù)庫升級事件 request.onupgradeneeded = function (event) { //通過事件對象的target.result屬性,拿到數(shù)據(jù)庫實(shí)例。 console.log(event); //request對象的result屬性上面,拿到一個(gè)IDBDatabase對象,它表示連接的數(shù)據(jù)庫 db = event.target.result; let objectStore; // 更好的寫發(fā)是判斷表是否存在 if (!db.objectStoreNames.contains('newIDB')) { /**新建數(shù)據(jù) * 新建對象倉庫(即新建表) * 新增一張叫做person的表格,主鍵是id * */ //主鍵(key)是默認(rèn)建立索引的屬性。如果沒有可以讓 IndexedDB 自動(dòng)生成主鍵db.createObjectStore('person',{ autoIncrement: true }) objectStore = db.createObjectStore('newIDB', {keyPath: 'id'}); /** * 新建索引 * 三個(gè)參數(shù)分別為索引名稱、索引所在的屬性、配置對象(說明該屬性是否包含重復(fù)的值) * **/ objectStore.createIndex('name', 'name', {unique: false}); objectStore.createIndex('email', 'email', {unique: true}); } }; /**新增數(shù)據(jù) * 新增數(shù)據(jù)指的是向?qū)ο髠}庫寫入數(shù)據(jù)記錄。這需要通過事務(wù)完成。 * 寫入數(shù)據(jù)需要新建一個(gè)事務(wù) * 新建時(shí)必須指定表格名稱和操作模式("只讀"或"讀寫") * 寫入操作是一個(gè)異步操作,通過監(jiān)聽連接對象的success事件和error事件,了解是否寫入成功。 * **/ function add() { //通過IDBTransaction.objectStore(name)方法,拿到 IDBObjectStore 對象,再通過表格對象的add()方法,向表格寫入一條記錄。 var request = db.transaction(['newIDB'], 'readwrite') .objectStore('newIDB') .add({id: 1, name: '張三', age: 24, email: 'zhangsan@example.com'}); request.onsuccess = function (event) { console.log('數(shù)據(jù)寫入成功'); }; request.onerror = function (event) { console.log('數(shù)據(jù)寫入失敗'); } } setTimeout(function () { console.log(db); add(); }, 2000); /** 讀取數(shù)據(jù) * *讀取數(shù)據(jù)也是通過事務(wù)完成。 * * * **/ function read() { //創(chuàng)建是務(wù) let transaction = db.transaction(['newIDB']); //拿到 IDBObjectStore 對象 let objectStore = transaction.objectStore('newIDB'); //objectStore.get()方法用于讀取數(shù)據(jù),參數(shù)是主鍵的值。 let request = objectStore.get(1); //失敗監(jiān)聽 request.onerror = function (event) { console.log('事務(wù)失敗'); db.close() }; //成功監(jiān)聽 request.onsuccess = function (event) { if (request.result) { console.log('Name: ' + request.result.name); console.log('Age: ' + request.result.age); console.log('Email: ' + request.result.email); } else { console.log('未獲得數(shù)據(jù)記錄'); } }; } setTimeout(function () { read(); }, 4000); /**遍歷數(shù)據(jù) *遍歷數(shù)據(jù)表格的所有記錄,要使用指針對象 IDBCursor。 * * */ function readAll() { let objectStore = db.transaction('newIDB').objectStore('newIDB'); //新建指針對象的openCursor()方法是一個(gè)異步操作,所以要監(jiān)聽success事件。 objectStore.openCursor().onsuccess = function (event) { let cursor = event.target.result; if (cursor) { console.log('Id: ' + cursor.key); console.log('Name: ' + cursor.value.name); console.log('Age: ' + cursor.value.age); console.log('Email: ' + cursor.value.email); cursor.continue(); } else { console.log('沒有更多數(shù)據(jù)了!'); } }; } setTimeout(function () { readAll(); }, 6000); /**跟新數(shù)據(jù) *更新數(shù)據(jù)要使用IDBObject.put()方法。 * * */ function update() { //put()方法自動(dòng)更新了主鍵為1的記錄。 let request = db.transaction(['newIDB'], 'readwrite') .objectStore('newIDB') .put({id: 1, name: '李四', age: 35, email: 'lisi@example.com'}); request.onsuccess = function (event) { console.log('數(shù)據(jù)更新成功'); }; request.onerror = function (event) { console.log('數(shù)據(jù)更新失敗'); db.close(); } } setTimeout(function () { update(); }, 8000); /**刪除數(shù)據(jù) *IDBObjectStore.delete()方法用于刪除記錄 * * **/ function remove() { let request = db.transaction(['newIDB'], 'readwrite') .objectStore('newIDB') .delete(1); request.onsuccess = function (event) { console.log('數(shù)據(jù)刪除成功'); }; } // remove(); /**使用索引 * 索引的意義在于,可以讓你搜索任意字段,也就是說從任意字段拿到數(shù)據(jù)記錄。如果不建立索引,默認(rèn)只能搜索主鍵(即從主鍵取值)。 * */ function search() { let request = db.transaction(['newIDB'], 'readonly') .objectStore('newIDB') .index('name') .get('李四'); request.onsuccess = function (e) { var result = e.target.result; if (result) { console.log('搜索成功') } else { console.log('搜索失敗') } } } // search(); </script> </body> </html> **注意:使用的時(shí)候鏈接數(shù)據(jù)庫,失敗或者完成其他操作關(guān)閉數(shù)據(jù)庫;**
本篇文章到這里就已經(jīng)全部結(jié)束了,更多其他精彩內(nèi)容可以關(guān)注PHP中文網(wǎng)的JavaScript視頻教程欄目!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com