简体   繁体   English

在由SQLite支持的Android ContentProvider中,应如何实现insertOrUpdate语义?

[英]In Android ContentProvider backed by SQLite, how should I implement insertOrUpdate semantics?

This is in the case where there is a UNIQUE column apart from the _id column. 在_id列之外还有UNIQUE列的情况下。

I can see three options: 我可以看到三个选项:

  1. Leave insert as insert-if-no-conflict, and update as update-if-present (this seems to be the "standard"), and perform all the work on the client to decide how to resolve this. 将insert保留为“ if-if-no-conflict”,将更新保留为“ update-if-present”(这似乎是“标准”),并在客户端上执行所有工作,以决定如何解决此问题。

  2. Change insert to insert-or-update-if-present and leave update as update-if-present. 将insert更改为insert-or-update-if-present,将update保留为update-if-present。 Does this break the standard ContentProvider contract? 这会违反标准的ContentProvider合同吗?

  3. Change update to update-or-insert-if-not-present and leave insert as insert-if-no-conflict. 将update更改为update-or-insert-if-present,然后将insert保留为insert-if-no-conflict。 This makes less sense than the second option above to me, so I'll probably not do this - but I suspect it's pretty similar in the end. 这对我来说比上面的第二个选项没有意义,因此我可能不会这样做-但我怀疑最后它还是很相似。

Additionally, if I do the first option, is there any advantage in doing 另外,如果我做第一个选择,这样做有什么好处吗?

  1. query then if count=1, replace, else insert 然后查询,如果count = 1,则替换,否则插入
  2. insert, if failed replace 插入,如果失败则替换
  3. replace, if failed insert. 如果插入失败,请更换。

I anticipate more updates than replaces overall, but am mainly concerned with making this maintainable, as I don't expect this to be a performance bottleneck. 我预计将有更多更新而不是整体替换,但主要是要使它具有可维护性,因为我认为这不会成为性能瓶颈。

My inclination will be to query and then update or insert based on if there is an existing record. 我的倾向是根据是否有现有记录来查询然后进行更新或插入。 You can use the existing ContentProvider methods for this - and it will be quite readable for subsequent developers to understand. 您可以为此使用现有的ContentProvider方法-以后的开发人员将很容易理解。

The downside is there's a couple of unnecessary DB hits. 缺点是有一些不必要的数据库命中。

The article talks through how you could modify the ContentProvider to 'implement' insertOrUpdate 本文讨论了如何将ContentProvider修改为“实现” insertOrUpdate。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何预填充ContentProvider支持的Android数据库 - How to Pre-populate An Android Database Backed by ContentProvider Android ContentProvider SQLite:SQLiteMisuseException - Android ContentProvider SQLite : SQLiteMisuseException 带有ContentProvider或SQLiteOpenHelper的Android Sqlite - Android Sqlite with ContentProvider or SQLiteOpenHelper 没有ContentProvider的Android Sqlite - Android Sqlite without ContentProvider 带有ContentProvider和BaaS的Android SQLite - Android SQLite with ContentProvider and BaaS ContentProvider Sqlite和许多用户。 我应该为用户创建单个数据库还是如何控制它 - ContentProvider Sqlite and many users. Should I create individual db`s for users or how to control that 我应该如何连接 Room、LiveData 和 ContentProvider? - How should I connect Room, LiveData and ContentProvider? Android:如何锁定可通过ContentProvider访问的SQLite数据库(或执行原子条件操作的其他方式) - Android: how can I lock SQLite database accessible with ContentProvider (or other way of executing atomic conditional operations) Android:使用 SQLiteOpenHelper。 我应该使用 ContentProvider 吗? - Android: Using SQLiteOpenHelper. Should I use a ContentProvider? 在Android中创建RESTful ContentProvider时,SQLite数据库中的唯一ID是否应与RESTful服务中的Key相匹配? - When creating a RESTful ContentProvider in Android, should the unique id in the SQLite database match the Key in the RESTful service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM