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
|
---
title: IDBObjectStore.autoIncrement
slug: Web/API/IDBObjectStore/autoIncrement
translation_of: Web/API/IDBObjectStore/autoIncrement
---
<p>{{ APIRef("IndexedDB") }}</p>
<div>
<p>{{domxref("IDBObjectStore")}}的只读属性<strong><code>autoIncrement</code></strong>接口返回当前objectStore的自增标记值(true或false)。</p>
<p>什么是自增呢?熟悉SQL的朋友应该知道,SQL数据里面的字段可以设置自增,当一条记录被插入时,不必传入该字段,新记录的该字段值会在前面一条记录该字段值的基础上加1.而indexedDB里面的autoIncrement的同样的意义。(译者注)</p>
<p>注意:每个objectStore的auto increment计数器是分开独立的。</p>
<p>{{AvailableInWorkers}}</p>
</div>
<h2 id="句法">句法</h2>
<pre class="syntaxbox">var <em>myAutoIncrement</em> = <em>objectStore</em>.autoIncrement;</pre>
<h3 id="Value">Value</h3>
<p><span style="line-height: 1.5;">{{domxref("Boolean")}}:</span></p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">值</th>
<th scope="col">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>true</code></td>
<td>当前objectStore会自增</td>
</tr>
<tr>
<td><code>false</code></td>
<td>当前objectStore不会自增<br>
</td>
</tr>
</tbody>
</table>
<h2 id="例子">例子</h2>
<p>在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用<code>add()</code>向一个objectStore中添加了一些数据。在objectStore被创建之后,我们在console中打印了<span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;">objectStore.autoIncrement</span>的值。想查看完整的例子,请查看我们的<a href="https://github.com/mdn/to-do-notifications/" rel="noopener">To-do Notifications</a>应用(<a href="http://mdn.github.io/to-do-notifications/" rel="noopener">查看在线例子</a>)。</p>
<pre class="brush: js" style="font-size: 14px;">// Let us open our database
<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4);
</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span>
note.innerHTML += '<li>Database initialised.</li>';
// store the result of opening the database in the db variable.
// This is used a lot below
db = DBOpenRequest.result;
// Run the addData() function to add the data to the database
addData();
};
function addData() {
// Create a new object ready to insert into the IDB
var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ];
// open a read/write db transaction, ready for adding the data
var transaction = db.transaction(["toDoList"], "readwrite");
// report on the success of the transaction completing, when everything is done
transaction.oncomplete = function(event) {
note.innerHTML += '<li>Transaction completed.</li>';
};
transaction.onerror = function(event) {
note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};
// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");
console.log(objectStore.autoIncrement);
// Make a request to add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = function(event) {
// report the success of our request
note.innerHTML += '<li>Request successful.</li>';
};
};</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('IndexedDB', '#widl-IDBObjectStore-autoIncrement', 'autoIncrement')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-autoincrement", "autoIncrement")}}</td>
<td>{{Spec2("IndexedDB 2")}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<div>
<div class="hidden">本页的兼容性列表是通过结构化数据生成的。如果你想贡献数据,请check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们发pull request.</div>
<p>{{Compat("api.IDBObjectStore.autoIncrement")}}</p>
</div>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li>
<li>开始学习事务 transactions: {{domxref("IDBDatabase")}}</li>
<li>使用事务 transactions: {{domxref("IDBTransaction")}}</li>
<li>key值域range的使用: {{domxref("IDBKeyRange")}}</li>
<li>检索、修改: {{domxref("IDBObjectStore")}}</li>
<li>使用游标: {{domxref("IDBCursor")}}</li>
<li>相关例子: <a href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>
|