diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/idbtransaction/db | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/idbtransaction/db')
-rw-r--r-- | files/zh-cn/web/api/idbtransaction/db/index.html | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbtransaction/db/index.html b/files/zh-cn/web/api/idbtransaction/db/index.html new file mode 100644 index 0000000000..d281a22ed6 --- /dev/null +++ b/files/zh-cn/web/api/idbtransaction/db/index.html @@ -0,0 +1,112 @@ +--- +title: IDBTransaction.db +slug: Web/API/IDBTransaction/db +translation_of: Web/API/IDBTransaction/db +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBTransaction")}} 的只读属性接口 <strong><code>db</code></strong>。返回该事务所属的数据库连接。</p> + +<p>{{AvailableInWorkers}}</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>myDatabase</em> = <em>transaction</em>.db;</pre> + +<h3 id="值">值</h3> + +<p><span style="line-height: 1.5;">一个 {{domxref("IDBDatabase")}} 对象。</span></p> + +<h2 id="Example">Example</h2> + +<p>In the following code snippet, we open a read/write transaction on our database and add some data to an object store. Note also the functions attached to transaction event handlers to report on the outcome of the transaction opening in the event of success or failure. At the end, we return the associated database connection using <code>db</code>. For a full working example, see our <a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a><span style="line-height: 1.5;"> app (</span><a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></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 for being inserted 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 opening the transaction + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed: database modification finished.</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"); + + // add our newItem object to the object store + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // report the success of the request (this does not mean the item + // has been stored successfully in the DB - for that you need transaction.onsuccess) + note.innerHTML += '<li>Request successful.</li>'; + }; + + // Return the database (IDBDatabase) connection with which this transaction is associated + transaction.db; +};</pre> + +<h2 id="Specification">Specification</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBTransaction-db', 'db')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbtransaction-db", "db")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.IDBTransaction.db")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> |