let request = indexedDB.open('CSVThreeJs', 1); request.onerror = function(event) { console.log('打开数据库出错!'); }; request.onupgradeneeded = function(event) { console.log('创建执行'); let db = event.target.result; // 创建一个名为“scene_9527”的对象存储 let store = db.createObjectStore('scene_9527', { keyPath: 'uuid' }); }; 在上文中scene_9527添加uuid为1-100值为{name:'aaa'}
时间: 2024-03-21 15:43:04 浏览: 30
你的问题不是很明确,我不确定你是在询问什么。但是,我可以理解你的意思,你想在 `scene_9527` 对象存储中添加100个对象,每个对象都有一个 uuid 属性和一个 name 属性。如果是这样,你可以按照以下方式进行操作:
```
let request = indexedDB.open('CSVThreeJs', 1);
request.onerror = function(event) {
console.log('打开数据库出错!');
};
request.onupgradeneeded = function(event) {
console.log('创建执行');
let db = event.target.result;
let store = db.createObjectStore('scene_9527', { keyPath: 'uuid' });
};
request.onsuccess = function(event) {
let db = event.target.result;
let tx = db.transaction('scene_9527', 'readwrite');
let store = tx.objectStore('scene_9527');
for (let i = 1; i <= 100; i++) {
store.put({ uuid: i, name: 'aaa' });
}
tx.oncomplete = function() {
console.log('添加成功!');
};
tx.onerror = function() {
console.log('添加失败!');
};
};
```
这个代码片段中,我们首先创建了一个名为 `CSVThreeJs` 版本为 `1` 的 IndexedDB 数据库。如果数据库已经存在,则直接打开数据库。如果数据库不存在,则创建一个名为 `scene_9527` 的对象存储,并且使用 `uuid` 属性作为主键。在 `onsuccess` 回调函数中,我们使用事务来操作对象存储。循环100次,每次向对象存储中添加一个包含 `uuid` 属性和 `name` 属性的对象。最后,当事务完成时,我们可以在 `oncomplete` 回调函数中输出一条添加成功的消息。如果事务出现错误,则可以在 `onerror` 回调函数中输出一条添加失败的消息。
相关推荐
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)