1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Learn IndexedDB</title>

</head>

<body>



<script>

var db = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

var request, result, version = 2,

​ dbName = 'testDB',

​ osName = 'os1';

function createDB() {

​ request = db.open(dbName, version)

​ *//请求有三种状态,如下:*

​ request.onsuccess = function() { *// 打开数据库成功*

​ db = request.result;

console.log('open success');

​ }

​ request.onerror = function(*e*) { *// 打开数据库失败*

console.log(e.currentTarget.errormessage)

​ }

​ request.onupgradeneeded = function(*e*) { *//请求数据库版本变化时*

var store = null;

​ db = e.target.result;

console.log('upgradeneeded');

​ if (!db.objectStoreNames.contains(osName)) {

​ *// db.createObjectStore(osName, {autoIncrement: true}) // 创建的表的主建是自增型的*

​ store = db.createObjectStore(osName, { keyPath: 'id' }) *// 创建的表以字段为主建*

​ store.createIndex('idIndex', 'id', { unique: true }); *//创建索引字段id唯一*

​ store.createIndex('categoryIndex', 'category', { multiEntry: true }); *//创建索引字段为数组*

​ store.createIndex('hpIndex', 'hp', { unique: false });

​ }



​ }

​ }

​ createDB()

​ *// save data*

var data = [{

​ name: '史莱姆',

​ id: '001',

​ hp: 3,

​ category: ['怪物', '容易逃跑']

​ }, {

​ name: '小蝙蝠',

​ id: '002',

​ hp: 5,

​ category: ['怪物', '飞行']

​ }, {

​ name: '小恶魔',

​ id: '003',

​ hp: 9,

​ category: ['怪物', '恶魔']

​ }]

​ *// use IDBObjectStore.add to save data*

console.log(db);

function addData() {

if (!db) { alert("db"); }

var transaction, store;

​ transaction = db.transaction(osName, 'readwrite'); *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName) *///获取osName指定的 object store*

​ data.map(function(*o*) {

​ store.add(o)

​ *// request = store.add(o) //增加数据*

​ *// if (data.length - 1 === i) {*

​ *// request.onsuccess = function () {*

​ *// console.log('alreay add all data to db')*

​ *// showCurrentData()*

​ *// }*

​ *// }*

​ })

​ }

function getData(*id*) {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName);

var request = store.get(id);

​ request.onsuccess = function() {

console.log(request.result);

​ }

​ }

function getAllData() {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName);

var request = store.getAll();

​ request.onsuccess = function() {

console.log(request.result);

​ }

​ }

function updateData(*id*) {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName);

var request = store.get(id);

​ request.onsuccess = function() {

​ request = store.put({ *//更新即可以更新也可以添加,取决于关键字是否有重复*

​ name: '小花猫',

​ id: id,

​ hp: 9

​ });

​ }

​ }

function deleteData(*id*) {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName);

var request = store.delete(id);

​ request.onsuccess = function() {

console.log('delete success');

​ }

​ }

function clear() {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName);

var request = store.clear();

​ request.onsuccess = function() {

console.log('clear success');

​ }

​ }

function useCursorGetData() {

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName),

​ *// request=store.openCursor(); //创建游标*

​ *// request=store.openCursor(IDBKeyRange.only('002')); //指定游标KeyRange,only一个*

​ request = store.openCursor(null, 'prev');

​ request.onsuccess = function() {

var cursor = request.result;

​ if (cursor) {

console.log(cursor.value);

​ cursor.continue();

​ }

​ }

​ }

function useIndexGetData() { *// 索引只能查询数据,并不能操作数据*

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName),

​ *// request=store.openCursor(); //创建游标*

​ *// request=store.openCursor(IDBKeyRange.only('002')); //指定游标KeyRange,only一个*

​ index = store.index('categoryIndex');

​ request = index.getAll('飞行');

​ request.onsuccess = function() {

console.log(request.result);

​ }

​ }

function useIndexAndCursorOperateData() { *// 索引和游标结合,可以查询和操作数据*

var transaction = db.transaction(osName, 'readwrite'), *//打开一个事务,读写模式*

​ store = transaction.objectStore(osName),

​ *// request=store.openCursor(); //创建游标*

​ *// request=store.openCursor(IDBKeyRange.only('002')); //指定游标KeyRange,only一个*

​ *// index=store.index('categoryIndex');*

​ index = store.index('hpIndex');

​ *// request = index.openCursor(IDBKeyRange.upperBound(5)); // 5(包含5)以下*

​ request = index.openCursor(IDBKeyRange.bound(5,10,true,true)); *// 范围(5到10)*

​ request.onsuccess = function() {

var cursor = request.result,value=null;

​ if (cursor) {

​ *// if(cursor.value.id==='002'){*

​ *// cursor.update({ // 更新数据*

​ *// name: '小蝙蝠',*

​ *// id: '002',*

​ *// hp: 10,*

​ *// category:['怪物','飞行']*

​ *// })*

​ *// cursor.delete().onsuccess=function () { // 删除数据*

​ *// console.log('delete');*

​ *// }*

​ *// }*

​ value = cursor.value;

​ value.hp += 20;

​ cursor.update(value);

console.log(cursor.value);

​ cursor.continue();

​ }

​ }

​ }

</script>

</body>

</html>