3.4 读取数据

读取数据也是通过事务完成。

  1. function read() {
  2. var transaction = db.transaction(['person']);
  3. var objectStore = transaction.objectStore('person');
  4. var request = objectStore.get(1);
  5. request.onerror = function(event) {
  6. console.log('事务失败');
  7. };
  8. request.onsuccess = function( event) {
  9. if (request.result) {
  10. console.log('Name: ' + request.result.name);
  11. console.log('Age: ' + request.result.age);
  12. console.log('Email: ' + request.result.email);
  13. } else {
  14. console.log('未获得数据记录');
  15. }
  16. };
  17. }
  18. read();

上面代码中,objectStore.get()方法用于读取数据,参数是主键的值。