相关对象
IDBFactory
作用:用于创建或打开数据库,并管理数据库的版本
获取方式:var IDBFactory =window.indexedDB
IDBOpenDBRequest
作用:为请求对象,而对数据库的访问、操作都是基于请求的
获取方式:var IDBOpenDBRequest = window.indexedDB.open(name,version)
IDBDatabase
作用:为真正的数据库对象,封装了对数据库表的创建、编辑等功能
获取方式:基于请求对象IDBOpenDBRequest,当IDBOpenDBRequest申请成功时,该对象的result属性就是IDBDatabase数据库对象。
IDBOpenDBRequest.onsuccess= function (e){
var IDBDatabase =IDBOpenDBRequest.result;
//或者
var IDBDatabase =e.target.result;
}
关闭数据库:调用IDBDatabase对象的close()方法
IDBDatabase.close();
删除数据库:调用indexedDB对象的deleteDatabase方法
indexedDB.deleteDatabase(name);
IDBObjectStore
作用:相当于一张表,里面存储的每条数据和一个键相关联
初始化方式:在创建数据库的时候初始化IDBObjectStore,即在IDBOpenDBRequest. Onupgradeneeded事件中。
IDBOpenDBRequest.onupgradeneeded= function(e){
var db = e.target.result;
if(!db.objectStoreNames.contains('students')){
db.createObjectStore('students',{keyPath:"id"});
}
console.log('DB version changedto ' + version);
};
获取方式:需要通过事务获取
var transaction=db.transaction(['students']);
var objectStore=transaction.objectStore('students');
添加数据:通过IDBObjectStore对象的add方法添加
objectStore.add({
id:1001,
name:'huahua',
age:25
})
获取数据:通过IDBObjectStore对象的get方法通过键获取数据
function getDataByKey(db,storeName,value){
var transaction =db.transaction(storeName,'readwrite');
var store =transaction.objectStore(storeName);
var request = store.get(value);
request.onsuccess = function(e){
var student = e.target.result;
console.log(student.age);
}
}
删除数据:通过IDBObjectStore对象的delete方法根据键值删除数据
function deleteDataByKey(db,storeName,value){
vartransaction = db.transaction(storeName,'readwrite');
var store =transaction.objectStore(storeName);
store.delete(value);
}
清空数据:通过IDBObjectStore对象的clear方法清除数据
function clearObjectStore(db,storeName) {
var transaction = db.transaction(storeName,'readwrite');
var store =transaction.objectStore(storeName);
store.clear();
}
所有代码如下:
var iDBFactory = window.indexedDB;
var iDBOpenDBRequest = window.indexedDB.open('student',1);
iDBOpenDBRequest.onsuccess = function(){
var iDBDatabase = iDBOpenDBRequest.result;
var transaction = iDBDatabase.transaction(['student'],'readwrite');
var objectStore = transaction.objectStore('student');
objectStore.add({
id: 1001,
name: 'huahua',
age: 25
});
getDataByKey(iDBDatabase,'student',1001);
};
iDBOpenDBRequest.onerror = function(){
};
iDBOpenDBRequest.onupgradeneeded = function(e){
var db = e.target.result;
if(!db.objectStoreNames.contains('student')) {
db.createObjectStore('student',{
keyPath: 'id'
});
}
};
function getDataByKey(db, storeName, value){
var transaction = db.transaction(storeName, 'readwrite');
var store = transaction.objectStore(storeName);
var request = store.get(value);
request.onsuccess = function(e) {
var student = e.target.result;
console.log(student.age);
}
}
function deleteDataByKey(db, storeName, value){
var transaction = db.transaction(storeName, 'readwrite');
var store = transaction.objectStore(storeName);
store.delete(value);
}
function clearObjectStore(db,storeName) {
var transaction = db.transaction(storeName,'readwrite');
var store = transaction.objectStore(storeName);
store.clear();
}