导出和导入IndexedDB数据 [英] Exporting and importing IndexedDB data

查看:580
本文介绍了导出和导入IndexedDB数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己使用一个需要简单数据库的工具。这似乎是学习HTML5 IndexedDB API的好机会,但重要的是我不要在任何时候丢失数据。

I'm making a tool for my own use that needs a simple database. This seems like a good chance to learn the HTML5 IndexedDB API, but it's important that I don't lose data at any point.

我想备份浏览器的配置文件目录会做备份,但我也希望可以使用不同的计算机,因此导出和导入数据库会很好。有没有一种简单的方法来导出和导入IndexedDB数据库?浏览器特定的解决方案还可以。

I suppose backing up the browser's profile directory would do for a backup, but I'd also like to potentially work with different computers so exporting and importing the database would be nice. Is there an easy way to export and import IndexedDB databases? Browser-specific solutions are ok.

推荐答案

纯IndexedDB规范,但是,可以编写自己的方法来实现这一目标。

There is nothing like this available in the pure IndexedDB spec, however, it's possible to write your own methods that will accomplish this.

基本导入数据的步骤是


  1. 打开IndexedDB数据库

  2. 创建对象库

  3. 添加索引

  4. 遍历您的对象并逐个添加(通过添加 put operation)

  1. open an IndexedDB database
  2. create an object store
  3. add indexes
  4. loop through your objects and add them one by one (via an add or put operation)

要导出对象商店,您可以:

For exporting an object store you can:


  1. 打开一个光标,左边为零,每次打勾

  2. 添加 onsuccess 回调请求对象以捕获行值

  3. 每次成功回调将行推送到特权数组 var

  1. open up a cursor with zero as the left bound and at each tick
  2. add an onsuccess callback to the request object to capture the row value
  3. on each success callback push the row to a privileged array var.

最后一行将发出 null ,wh ich是一个你可以观察的状态,以确定光标已经耗尽所有记录并完成。发生这种情况时,您可以调用导出回调,传递表示对象存储备份的特权对象数组。

The final row will emit null, which is a state you can watch for to figure out when the cursor has exhausted all its records and is done. When that happens, you can call an export callback passing the privileged array of objects representing a backup of your object store.

这篇关于导出和导入IndexedDB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆