<html>
<head>
<script src="jquery-3.5.1.js"></script>
</head>
<body>
<script>
createOrOpenDb("teamtalk");
//新建或打开已有数据库
function createOrOpenDb(dbName){
var request = indexedDB.open(dbName, 1);
request.onsuccess = function(event) {
console.log('打开/创建 数据库成功');
};
request.onerror = function(event) {
console.log('打开/创建 数据库成功失败:' + event.target.error.message);
};
}
function insertData(db){
/**
* 第一个参数,存储空间的名称,即上面的customers。
* 第二个参数,keyPath值指定主键。
* autoIncrement指定了key值是否自增(当key值为默认的从1开始到2^53的整数时)。
* */
var objectStore = db.createObjectStore("customers" , { keyPath: "name" } );
/**
* 第一个参数,索引的名称。
* 第二个参数,指定索引对应列。
* 第三个属性, options对象,其中属性unique的值为true表示索引必须唯一。
* */
// objectStore.createIndex("name", "name", { unique: false });
objectStore.add({"name": 'a', "age": "10",ssn: "444-44-4444"});
objectStore.add({"name": 'b', "age": "20",ssn: "444-44-4442"});
}
function deleteData(db, storename, id){
var transaction = db.transaction(storename, 'readwrite');//获取事务
var store = transaction.objectStore(storename); // 得到对应的object store
store.delete(id);// 根据id删除对应store中的数据
}
function readData(db, storename, id){
var transaction = db.transaction(storename, 'readwrite');
var objectStore = transaction.objectStore(storename);
var req = objectStore.get(id);
req.onsuccess = function(e) {
var employee = e.target.result;//得到对象
debugger
};
}
function updateData(db, storename, id){
var transaction = db.transaction(storename, 'readwrite'); //获取事务
var store = transaction.objectStore(storename);// 得到指定的object store
var req = store.get(id);//根据id查找指定的对象
req.onsuccess = function(e) {
var employee = e.target.result;//得到对象
employee.age = 19;//修改值
store.put(employee);//将修改之后的值放回指定的object store中
};
}
</script>
</body>
</html>