如何将离线 HTML5 Web 数据库与集中数据库同步
我希望能够在 HTML5 (iPad) Web 应用程序中执行以下操作:
- 其大小可能小于 50Mb)
- 将数据上传到在线数据库(如果我要在 SQLite 等中构建在线数据库, 将数据的子集或完整副本保存到离线 Web 数据库
- (超出 3G 网络覆盖范围)
- 对下载的数据执行一系列分析类型计算
- 将计算的参数保存到离线 Web 数据库
- 重复,保存不同的参数集长时间内进行几个不同的离线分析类型计算会话
- (返回到有 3G 网络覆盖的区域)
- 将保存的参数从我的离线网络数据库同步到中央在线数据库,
直到最后一步,我对每一步都很满意。 我正在尝试查找有关
是否可以将离线网络数据库与中央数据库同步的信息,但找不到涵盖该主题的任何内容。可以这样做吗?如果是这样,您能否提供相关信息的链接,或者详细描述它如何为我的特定应用程序实现它?
提前致谢
I'd like to be able to do the following in a HTML5 (iPad) web app:
- upload data to an online database (which would be probably <50Mb in size if I was to build the online database in something like SQLite)
- extract either a subset or a full copy of data to an offline webdatabase
- (travel out of 3G network coverage range)
- perform a bunch of analytic-type calculations on the downloaded data
- save parameters for my calculations to the offline webdatabase
- repeat, saving different parameter sets for several different offline analytic-type calculation sessions over an extended period
- (head back into areas with 3G network coverage)
- sync the saved parameters from my offline webdatabase to the central, online database
I'm comfortable with every step up till the last one...
I'm trying to find information on whether it's possible to sync an offline webdatabase with a central database, but can't find anything covering the topic. Is it possible to do this? If so, could you please supply link/s to information on it, OR describe how it would work in enough detail to implement it for my specific app?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没有专门使用过 HTML5 本地数据库,但我使用过需要离线更新并重新同步到中央数据存储的移动设备。
无论数据集是在服务器上还是在离线客户端上创建,我都会确保其主键是 UUID。我还确保每次更新记录时都为其添加时间戳。
我也没有提及上次同步离线客户端的时间。
因此,当重新同步到中央数据库时,我首先查询离线客户端以获取自上次同步以来已更改的记录。然后,我查询中央数据库以确定自上次同步以来这些记录是否已更改。
如果它们在中央数据库上没有更改,我会使用离线客户端的数据更新它们。如果服务器上的记录自上次同步以来发生了更改,我会将它们更新到客户端。
如果 UUID 在中央服务器上不存在,但在离线客户端上存在,则我将其插入,反之亦然。
为了清除记录,我创建了一个“清除”列,当运行 sysnc 查询时,我从每个数据库中删除该记录(或将其标记为非活动状态,具体取决于应用程序要求)。
如果自上次更新以来两条记录都发生了更改,我必须依靠用户输入进行协调,或者指定哪个记录“获胜”的规则。
我通常不信任内置的数据库导入功能,除非我要导入到完全空的数据库中。
I haven't worked specifically with HTML5 local databases, but I have worked with mobile devices that require offline updates and resyncing to a central data store.
Whether the dataset is created on the server or on the offline client, I make sure its primary key is a UUID. I also make sure to timestamp the record each time its updated.
I also make not of when the last time the offline client was synced.
So, when resyncing to the central database, I first query the offline client for records that have changed since the last sync. I then query the central database to determine if any of those records have changed since the last sync.
If they haven't changed on the central database, I update them with the data from the offline client. If the records on the server have changed since last sync, I update them to the client.
If the UUID does not exist on the central server but does on the offline client, I insert it, and vice versa.
To purge records, I create a "purge" column, and when the sysnc query is run, I delete the record from each database (or mark it as inactive, depending on application requirements).
If both records have changed since last update, I have to either rely on user input to reconcile or a rule that specifies which record "wins".
I usually don't trust built-in database import functions, unless I'm importing into a completely empty database.
步骤:
这种方法实际上可以适用于任何数据库组合,只要一侧有数据转换器即可。
Steps:
This method can actually work on any combination of databases, provided there is a data convertor on one side.
在我看来,从我访问过的几个网站来看,(只要您使用 SQLite 作为服务器数据库)应该是可能的。
HTML5 Web 数据库也使用 SQLite(尽管并非所有浏览器都支持它,并且 W3C 似乎已经放弃了对它的支持)
所以...
如果您使用 .dump 命令导出数据,然后使用 $sqlite mydb 将数据导入到 Web 数据库中。分贝mydump.sql 语法你应该能够通过 php 或 java 后端来做到这一点?
然后,当您想要将“离线”数据同步到服务器时,只需执行相反的从 Web 数据库转储到 dump.sql 文件并导入到服务器数据库的
操作。此站点解释了从 SQLite 转储导出和导入
源:转储和恢复 SQLite DB
It looks to me, from a few sites I visited, that (as long as you are using SQLite for your Server db) it should be possible.
HTML5 webdatabases also use SQLite (although not all browsers support it and W3C seems to have dropped support for it)
so...
If you export the data using the .dump command and then import the data into the webdatabase using the $sqlite mydb.db < mydump.sql syntax you should be able to do this with some fidgeting with a php or java backend?
Then when you want to sync the "offline" data to your server just do the opposite dump from the webdatabase to a dump.sql file and import to the server database
This site explains exporting to and importing from SQLite dumps
SOURCE: dumping and restoring an SQLite DB
HTML5支持浏览器db SQLite,我在Mozilla和chrome中尝试过,效果很好。
我还有一个要求,我有一些离线表单,用户输入数据并单击保存,它保存在本地浏览器数据库中,稍后当用户与服务器同步或上线时,他可以单击同步按钮来实际同步数据从浏览器数据库任何其他数据源。
HTML5 supports browser db SQLite , I have tried in Mozilla and chrome, and it works fine.
I also have a requirement where I have some offline form, user punches the data and click on save, it saves in local browser db, and later when user syncs with the server or comes online, he can click on sync button which actually sync data from browser db any other datasource.