成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

indexedDB添加,刪除,獲取,修改

MycLambert / 3270人閱讀

摘要:在版本測(cè)試正常編寫(xiě)涉及在線演示代碼在的中查看數(shù)據(jù)庫(kù)信息添加數(shù)據(jù)獲取數(shù)據(jù)請(qǐng)輸入刪除數(shù)據(jù)請(qǐng)輸入更新數(shù)據(jù)請(qǐng)輸入代碼代碼不支持不存在當(dāng)前數(shù)據(jù)庫(kù)時(shí),即為創(chuàng)建參數(shù)數(shù)據(jù)庫(kù)名稱(chēng),版本號(hào)整數(shù)數(shù)據(jù)庫(kù)已存在,打開(kāi)成功當(dāng)數(shù)據(jù)庫(kù)不存在時(shí),創(chuàng)建數(shù)據(jù)庫(kù)會(huì)觸發(fā)事件當(dāng)指定

[toc]

在chrome(版本 70.0.3538.110)測(cè)試正常
編寫(xiě)涉及:css, html, js

在線演示codepen

html代碼

indexedDB

在 DevTools 的 Application 中查看數(shù)據(jù)庫(kù)信息

css代碼
button {
    margin: 10px 0;
    padding: 8px 10px;
    border: 0;
    color: #fff;
    background-color: rgb(7, 196, 230);
}
input{
    padding: 8px 5px;
    border: rgb(7, 196, 230) solid 1px
}
JavaScript代碼
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
if (!window.indexedDB) {
    alert("不支持indexDB")
}

const DATA_BASE = "TEST_DB"
let db = {}

/**
    1. 不存在當(dāng)前數(shù)據(jù)庫(kù)時(shí),即為創(chuàng)建
    2. open參數(shù):數(shù)據(jù)庫(kù)名稱(chēng),版本號(hào)(整數(shù))
*/
const request = window.indexedDB.open(DATA_BASE, 1) 

request.onerror = function (e) {
    console.error("error", e)
}

/**
    1. 數(shù)據(jù)庫(kù)已存在,打開(kāi)成功
*/
request.onsuccess = function (event) {
    db = event.target.result
    console.log("execute onsuccess");
};

/**
    1. 當(dāng)數(shù)據(jù)庫(kù)不存在時(shí),創(chuàng)建數(shù)據(jù)庫(kù)會(huì)觸發(fā) onupgradeneeded 事件
    2. 當(dāng)指定的版本號(hào)高于當(dāng)前時(shí)會(huì)直接觸發(fā) onupgradeneeded 事件
    3. 唯一可以修改數(shù)據(jù)庫(kù)結(jié)構(gòu)的事件
*/
request.onupgradeneeded = function (event) {
    /**
        1. 保存 IDBDataBase 接口(數(shù)據(jù)庫(kù))
    */
    db = event.target.result
    console.log("execute onupgradeneeded");

    /**
        1.使用鍵生成器,測(cè)試時(shí),去除注釋即可
    */
    // const objectStore = db.createObjectStore("users", { autoIncrement : true })
    // objectStore.add({name: "123"});
    // objectStore.add("123");

    /**
        1. 為當(dāng)前數(shù)據(jù)庫(kù)創(chuàng)建對(duì)象倉(cāng)庫(kù)(表) 
        2. 這里的keyPath 相當(dāng)于主鍵
        3. createIndex(indexName, keyPath, {unique | multiEntry | locale})
    */
    const objectStore = db.createObjectStore("users", { keyPath: "id" })
    objectStore.createIndex("name", "name", { unique: false })
    objectStore.createIndex("age", "age", { unique: false })

    /**
        1. 確保初始化數(shù)據(jù)的時(shí)候,對(duì)象倉(cāng)庫(kù)已經(jīng)創(chuàng)建完畢
    */
    objectStore.transaction.oncomplete = function(event) {
        const transaction = db.transaction(["users"], "readwrite")
        const objStore = transaction.objectStore("users")
        objStore.add({
            id: 12, 
            name: `hew-${Math.random()}` ,
            age: parseInt( Math.random() * 100, 10)
        })
    }
}

/**
    1. 所有數(shù)據(jù)庫(kù)操作都基于事務(wù)
    2. 事務(wù)三種模式:readonly,readwrite,versionchange
    3. 修改數(shù)據(jù)庫(kù)模式或結(jié)構(gòu)——包括新建或刪除對(duì)象倉(cāng)庫(kù)或索引,只能用 versionchange 模式
*/
function addDBData() {
    const transaction = db.transaction(["users"], "readwrite")
    const objectStore = transaction.objectStore("users")
    const request = objectStore.add({ 
        id: Math.random() * 10, 
        name: `hew-${Math.random()}` ,
        age: parseInt( Math.random() * 100, 10)
    })

    transaction.oncomplete = function (event) {
        console.log("transaction add complete")
    }

    transaction.onerror = function (error) {
        console.error("add error", error)
    }

    request.onsuccess = function (event) {
        console.log("add complete")
    }
}

function getDBData() {
    const id = parseFloat(document.getElementById("getID").value)
    const transaction = db.transaction(["users"], "readwrite")
    const objectStore = transaction.objectStore("users")

    /**
        1. 注意數(shù)據(jù)類(lèi)型
    */
    const request = objectStore.get(id)

    request.onsuccess = function (event) {
        console.log("get complete", event.target.result)
        document.getElementById("display").innerHTML = JSON.stringify(request.result)
    }

    request.onerror = function (err) {
        console.error("get error", err)
    }
}

function updateDBData() {
    const id = parseFloat(document.getElementById("updateID").value)
    const transaction = db.transaction(["users"], "readwrite")
    const objectStore = transaction.objectStore("users")
    const request = objectStore.get(id)

    request.onsuccess = function (event) {
        const d = event.target.result
        d.name = "name name"
        const objectStoreUpdate = objectStore.put(d)
        objectStoreUpdate.onsuccess = function (e) {
            console.log("update success")
        }
        
        document.getElementById("display").innerHTML = "update success"
    }

    request.onerror = function (err) {
        console.error("get error", err)
    }
        
}

function delDBData() {
    const id = parseFloat(document.getElementById("delID").value)
    const request = db.transaction(["users"], "readwrite").objectStore("users").delete(id)

    request.onsuccess = function() {
        console.log("delete complete", id);
    }
}
參考

若有疑問(wèn)或錯(cuò)誤,請(qǐng)留言,謝謝!Github blog issues

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/99697.html

相關(guān)文章

  • 很全很全的前端本地存儲(chǔ)講解

    摘要:正文開(kāi)始三種本地存儲(chǔ)方式前言網(wǎng)絡(luò)早期最大的問(wèn)題之一是如何管理狀態(tài)。這個(gè)特點(diǎn)很重要,因?yàn)檫@關(guān)系到什么樣的數(shù)據(jù)適合存儲(chǔ)在中。特點(diǎn)生命周期持久化的本地存儲(chǔ),除非主動(dòng)刪除數(shù)據(jù),否則數(shù)據(jù)是永遠(yuǎn)不會(huì)過(guò)期的。 最近一直在搞基礎(chǔ)的東西,弄了一個(gè)持續(xù)更新的github筆記,可以去看看,誠(chéng)意之作(本來(lái)就是寫(xiě)給自己看的……)鏈接地址:Front-End-Basics 此篇文章的地址:三種本地存儲(chǔ)方式 ...

    Cciradih 評(píng)論0 收藏0
  • HTML5 進(jìn)階系列:indexedDB 數(shù)據(jù)庫(kù)

    摘要:版本號(hào)必須為整數(shù)更新版本,打開(kāi)版本為的數(shù)據(jù)庫(kù)新數(shù)據(jù)庫(kù)版本號(hào)為我們通過(guò)監(jiān)聽(tīng)請(qǐng)求對(duì)象的事件來(lái)定義數(shù)據(jù)庫(kù)版本更新時(shí)執(zhí)行的方法。 前言 在 HTML5 的本地存儲(chǔ)中,有一種叫 indexedDB 的數(shù)據(jù)庫(kù),該數(shù)據(jù)庫(kù)是一種存儲(chǔ)在客戶端本地的 NoSQL 數(shù)據(jù)庫(kù),它可以存儲(chǔ)大量的數(shù)據(jù)。從上篇:HTML5 進(jìn)階系列:web Storage ,我們知道 web Storage 可以方便靈活的在本地存取...

    philadelphia 評(píng)論0 收藏0
  • 使用IndexedDB做前端日志持久化

    摘要:?jiǎn)栴}頁(yè)面如果表現(xiàn)不符合預(yù)期,前端工程師在沒(méi)有日志的情況下,很難。所以就需要針對(duì)必要的步驟記錄日志,并上傳。只能在回調(diào)函數(shù)中被調(diào)用。事物關(guān)閉日志對(duì)象。處理異常利用的重建因?yàn)橹荒茉谥姓{(diào)用。 問(wèn)題 頁(yè)面如果表現(xiàn)不符合預(yù)期,前端工程師在沒(méi)有 javascript 日志的情況下,很難 debug。所以就需要針對(duì)必要的步驟記錄日志,并上傳。但是每記錄一條日志就上傳并不是一個(gè)合適的選擇,譬如如果生成...

    CoderDock 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<