代码拉取完成,页面将自动刷新
function openDB(dbname, version = 1) {
return new Promise((resolve, reject) => {
// 兼容浏览器
var indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB;
let db;
// 打开数据库,若没有就会创建
const request = indexedDB.open(dbname, version);
// 数据库打开成功回调
request.onsuccess = function (event) {
db = event.target.result; // 数据库对象
console.log("数据库打开成功");
resolve(db);
};
// 数据库打开失败的回调
request.onerror = function (event) {
console.log("数据库打开失败");
};
// 数据库有更新的时候的回调
request.onupgradeneeded = function (event) {
// 数据库创建或升级的时候回调
console.log("onupgradeneeded");
db = event.target.result;
var objectStore;
// 创建数据库
objectStore = db.createObjectStore("user", {
keyPath: "uuid", // 主键
autoIncrement: true, // 自增
});
// 创建索引,在后面查询数据的时候可以根据索引查
objectStore.createIndex("uuid", "uuid", {unique: true});
objectStore.createIndex("name", "name", {unique: false});
objectStore.createIndex("age", "age", {unique: false});
}
})
}
// 插入数据
function addData(db, storeName, data) {
var request = db
.transaction([storeName], "readwrite") // 事务对象,指定表格名称和操作的模式(“只读”或“读写”)
.objectStore(storeName) // 创建对象
.add(data);
request.onsuccess = function (event) {
console.log("数据写入成功");
}
request.onerror = function (event) {
console.log("数据写入失败");
}
}
// 通过主键查询数据
function getDataByKey(db, storeName, key) {
var transaction = db.transaction([storeName]); // 声明事务
var objectStore = transaction.objectStore(storeName); // 仓库对象
var request = objectStore.get(key); // 通过主键获取数据 getAll 查询所有数据
request.onerror = function (event) {
console.log("事务失败")
};
request.onsuccess = function (event) {
console.log("主键查询结果:", request.result);
}
}
// 通过游标查询数据
function cursorGetData(db, storeName) {
let list = [];
var store = db
.transaction(storeName,"readwrite") // 事务
.objectStore(storeName); // 仓库对象
var request = store.openCursor(); // 指针对象
// 游标开启成功,逐行读取数据
request.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor){
// 必须要检查
list.push(cursor.value);
cursor.continue(); // 遍历存储对象中的所有内容
}else {
console.log("游标读取的数据:",list);
}
};
}
// 通过索引查询数据
function getDataByIndex(db,storeName,indexName,indexValue) {
var store = db.transaction(storeName,"readwrite").objectStore(storeName);
var request = store.index(indexName).get(indexValue);
request.onerror = function (){
console.log("事务失败")
};
request.onsuccess = function (e) {
var result = e.target.result;
console.log("索引插叙结果:",result);
};
}
// 通过索引和游标查询数据
function cursorGetDataByIndex(db, storeName, indexName, indexValue) {
let list = [];
var store = db.transaction(storeName,"readwrite").objectStore(storeName);
var request = store
.index(indexName) // 索引对象
.openCursor(IDBKeyRange.only(indexValue)); // 指针对象
request.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor){
// 必须检查
list.push(cursor.value);
cursor.continue(); // 遍历循环存储对象中的所有内容
} else {
console.log("游标索引查询的结果:",list);
}
};
request.onerror = function (e){};
}
// 通过索引和游标分页查询
function cursorGetDataByIndexAndPage(db,storeName,indexName,indexValue,page,pageSize) {
let list = [];
let counter = 0; // 计时器
let advanced = true; // 是否跳过多少条数据
var store = db.transaction(storeName,"readwrite").objectStore(storeName); // 仓库
var request = store
.index(indexName) // 索引对象
.openCursor(IDBKeyRange.only(indexValue)); // 指针对象
request.onsuccess = function (e) {
var cursor = e.target.result;
if (page>1 && advanced){
advanced = false;
cursor.advance((page-1) * pageSize); // 跳过多少条
return;
}
if (cursor){
// 必须检查
list.push(cursor.value);
counter++;
if (counter < pageSize){
cursor.continue();
} else {
cursor = null;
console.log("分页查询结果:",list);
}
} else {
console.log("分页查询结果为:",list);
}
};
request.onerror = function (e) {};
}
// 更新数据
function updateDB(db,storeName,data) {
var request = db
.transaction([storeName],"readwrite")
.objectStore(storeName)
.put(data);
request.onerror = function () {
console.log("更新失败")
};
request.onsuccess = function (){
console.log("更新成功")
};
}
// 通过主键删除数据
function deleteDB(db,storeName,id) {
var request = db
.transaction([storeName],"readwrite")
.objectStore(storeName)
.delete(id);
request.onsuccess = function () {
console.log("删除成功")
};
request.onerror = function () {
console.log("删除失败")
};
}
// 通过索引和游标删除数据
function cursorDelete(db, storeName, indexName, indexValue) {
var store = db.transaction([storeName],"readwrite").objectStore(storeName);
var request = store.index(indexName).openCursor(IDBKeyRange.only(indexValue));
request.onsuccess = function (e) {
var cursor = e.target.result;
var deleteRequest;
if(cursor){
deleteRequest = cursor.delete();
deleteRequest.onerror = function (){
console.log("游标删除失败");
};
deleteRequest.onsuccess = function (){
console.log("游标删除成功")
};
cursor.continue();
}
};
}
// 关闭数据库
function closeDB(db) {
db.close;
console.log("数据库关闭")
}
// 删除数据库
function deleteDBAll(dbName) {
console.log(dbName);
var request = window.indexedDB.deleteDatabase(dbName);
request.onsuccess = function (event){
console.log("删除成功")
};
request.onerror = function (event){
console.log("删除失败")
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。