同步数据库时区分数据的方法?
我有一个网络应用程序,我可以创建一些注释,每次创建新注释时,它都会插入到带有 auto_increment id 的表中。 (很明显)
现在我想开发一个Android应用程序,我也可以创建笔记(将它们保存在本地sqlite中),然后将这些笔记与服务器同步。
问题是,当我在手机中创建笔记时,它们将有自己的 auto_increment id,很多时候与服务器中的笔记相同!
我不关心有重复的笔记(实际上我不认为有办法区分新笔记是否重复,因为它们没有一些物理ID),问题是它们是否有相同的ID (主键),我将无法将它们插入服务器。
有什么建议吗?
I have a web app which I can create some notes, each time I create a new note, it will insert to a table with an auto_increment id. (quite obvious)
Now I want to develop an android app which I can create notes too (save them locally in sqlite), and then syncronize those notes with the server.
The problem is, when I create notes in my phone they will have their own auto_increment id which many times will be the same with those notes in server!
I don't care to have duplicated notes (actually I don't think there is a way to differentiate if the new note is duplicated or not, because they don't have some physical id), the problem is if they have same id (primary key), I won't be able to insert them to the server.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 UUID 作为笔记的键。
这样,每个条目都应该有一个唯一的 ID,无论是在服务器上还是在客户端上创建。
要创建 UUID,您可以使用 UUID.randomUUID()。
You could use an UUID as a key for your note.
That way, each entry should have an unique id, be it created on the server or on the client.
To create a UUID, you can use UUID.randomUUID().
最明显的解决方案是除了数据库的 auto_increment_id 之外,还为每个注释提供自己唯一的哈希值或 GUID。
然后,您可以使用这些唯一值作为同步的基础,并与每个表中的“上次同步”时间戳结合使用,以便您知道需要同步哪些数据,并可以轻松确定数据是否已存在于目标中(并且应该更新)或者是否是新注释。
The most obvious solution would be to give each note its own unique hash or GUID in addition to the database's auto_increment_id.
You'd then use these unique values as the basis for synchronisation in conjunction with a "last synced" timestamp in each of the tables so that you know what data needs to be synced and can easily determine if the data already exists in the destination (and should be updated) or whether it's a new note.
很抱歉,我认为您的数据库结构是错误的。您不能以这种方式使用自动增量字段,不同的数据库具有断开的架构。自动增量值是为特定用途而创建的,如果您需要像这样合并两个表,则必须实现不同的逻辑。使用note_id以唯一的方式标识一条笔记,使用更多的数据(即用户id、设备id等)来使这个id唯一。在这种情况下,自动增量最多只会给你带来一个混乱的架构
I'm sorry but i think that your DB structure is wrong. You cannot use autoincrement field in this way, different DBs with a disconnected architecture. Autoincrement values are created for a specific use, if you need to merge two tables like this, you have to implement a different logic. Use a note_id to identify a note in a unique way, using more data (i.e. the user id, the device id etc.) to make this id unique. Autoincrement will only give you a messy architecture at best in this scenario