如何将 Android 数据库与在线 SQL Server 同步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11299208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Synchronize Android Database with an online SQL Server?
提问by Wassim Taher
I am developing an Android App that stores different types of data in the built-in SQLite provided by the Android Platform.
我正在开发一个 Android 应用程序,它在 Android 平台提供的内置 SQLite 中存储不同类型的数据。
Inside the App I have placed a "Sync" button which is supposed to Sync the data between the local SQLite Database, with an Online SQL Server database on my server.
在应用程序中,我放置了一个“同步”按钮,该按钮用于同步本地 SQLite 数据库与服务器上的在线 SQL Server 数据库之间的数据。
What is the workaround to handle this? This feature can be found in Google Calendar, where you can view the calendar's events on your mobile, and when you add a new event and Sync the data, you can view the updated data by going to your online account too.
处理此问题的解决方法是什么?这个功能可以在谷歌日历中找到,你可以在手机上查看日历的事件,当你添加一个新的事件并同步数据时,你也可以通过访问你的在线帐户来查看更新的数据。
Note: I don't want to centralize my database online, because I also want to give the mobile users the ability to use the App without internet connection.
注意:我不想在线集中我的数据库,因为我也想让移动用户能够在没有互联网连接的情况下使用该应用程序。
回答by tiguchi
You should take a look at the SampleSyncAdapter project in the Android SDK demos (Transferring Data Using Sync Adapters). It shows you how to do synchronization "Android style", which requires no user interaction by manually tapping a sync button all the time.
您应该查看 Android SDK 演示(使用同步适配器传输数据)中的 SampleSyncAdapter 项目。它向您展示了如何进行“Android 风格”同步,通过一直手动点击同步按钮,无需用户交互。
On top of that you need to write server software that is able to provide your SyncAdapter with a "delta" of all changes since the last commit. The most basic approach is keeping a "last synchronized" time stamp in your app which has to come from the server, otherwise you could get into trouble because of time differences between client and server. Normalize all time stamps as GMT or any timezone of your choice, but stick with that decision. If your app needs to display a time stamp in local time, then you need to use the Java Calendar and TimeZone class for converting the normalized time stamp into local time.
最重要的是,您需要编写服务器软件,该软件能够为您的 SyncAdapter 提供自上次提交以来所有更改的“增量”。最基本的方法是在您的应用程序中保留一个“上次同步”时间戳,该时间戳必须来自服务器,否则您可能会因为客户端和服务器之间的时间差异而陷入困境。将所有时间戳标准化为 GMT 或您选择的任何时区,但坚持该决定。如果您的应用程序需要以本地时间显示时间戳,那么您需要使用 Java Calendar 和 TimeZone 类将规范化的时间戳转换为本地时间。
Your app also needs to flag changed local records as "dirty", so your SyncAdapter knows that it needs to upload these changed or new records to the server.
您的应用程序还需要将更改的本地记录标记为“脏”,以便您的 SyncAdapter 知道它需要将这些更改的或新的记录上传到服务器。
Following minimum features are needed for your server software:
您的服务器软件至少需要以下功能:
- Update existing record(s) function
- Add new record(s) function
- Get updated records since last synchronization (app provides time stamp)
- Get new records since last synchronization (app provides time stamp)
- 更新现有记录功能
- 添加新记录功能
- 获取自上次同步以来的更新记录(应用提供时间戳)
- 获取自上次同步以来的新记录(应用程序提供时间戳)
You also may want to read through some Google API (like Google Calendar) for getting an idea of how all of this works and how to design the server API interface for the communication.
您可能还想通读一些 Google API(如 Google 日历)以了解所有这些是如何工作的以及如何设计用于通信的服务器 API 接口。