js操作indexedDB增删改查示例
生活随笔
收集整理的這篇文章主要介紹了
js操作indexedDB增删改查示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
js操作indexedDB增刪改查示例
if ('indexedDB' in window) {// 如果數(shù)據(jù)庫不存在則創(chuàng)建,如果存在但是version更大,會自動升級不會復(fù)制原來的版本var req = indexedDB.open("TestDB", 1);req.onupgradeneeded = function(e) {var db = req.result;// var store = db.createObjectStore("student", {autoIncrement: true}); 使用自增鍵// 創(chuàng)建student表var store = db.createObjectStore("student", {keyPath: 'id'});// 設(shè)置id為主鍵store.createIndex('student_id_unqiue','id', {unique: true});}req.onsuccess = function(event) {var students = [{id: 1, name: '小葉', age: '11'},{id: 2, name: '小王', age: '12'},{id: 3, name: '小張', age: '13'}];var db = event.target.result;// var transaction = db.transaction('student', 'readwrite');var transaction = db.transaction(['student'], 'readwrite');transaction.onsuccess = function(event) {console.log('[Transaction] 好了!');};var studentsStore = transaction.objectStore('student');students.forEach(function(student){var db_op_req = studentsStore.add(student);db_op_req.onsuccess = function() {console.log("存好了");}});studentsStore.count().onsuccess = function(event) {console.log('學(xué)生個數(shù)', event.target.result);};// 獲取id為1的學(xué)生studentsStore.get(1).onsuccess = function(event) {console.log('id為1的學(xué)生', event.target.result);};// 更新id為1的學(xué)生students[0].name = '小小葉';studentsStore.put(students[0]).onsuccess = function(event) {console.log('更新id為1的學(xué)生姓名', event.target.result);};// 刪除id為2的學(xué)生studentsStore.delete(2).onsuccess = function(event) {console.log('id為2的學(xué)生已經(jīng)刪除');};}req.onerror = function() {console.log("數(shù)據(jù)庫出錯");} }else{console.log('你的瀏覽器不支持IndexedDB'); }轉(zhuǎn)載于:https://www.cnblogs.com/ye-hcj/p/10353187.html
總結(jié)
以上是生活随笔為你收集整理的js操作indexedDB增删改查示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓网页离线保存_Android之 -W
- 下一篇: Superset单点登录调整源码