简体   繁体   English

带有ContentProvider或SQLiteOpenHelper的Android Sqlite

[英]Android Sqlite with ContentProvider or SQLiteOpenHelper

I was working around with android db and met 2 methods of working: 我正在研究android db,遇到了2种工作方法:

1) Using just SqliteOpenHelper 1)仅使用SqliteOpenHelper

2) Using ContentProvider and SqliteOpenHelper 2)使用ContentProviderSqliteOpenHelper

Is there any advantage using one or other ? 使用一个或另一个有什么好处吗?

This question is primarily opinion based, but to sum it up: 这个问题主要是基于意见的,但可以总结一下:

1) Using just SqliteOpenHelper 1)仅使用SqliteOpenHelper

You will have to manage the db access yourself. 您将必须自己管理数据库访问。 Provide methods for CRUD access and have full control. 提供CRUD访问的方法并具有完全控制权。 Updates can and should still be handled with SqliteOpenHelper . 可以并且仍然应该使用SqliteOpenHelper处理SqliteOpenHelper

2) Using ContentProvider and SqliteOpenHelper 2)使用ContentProvider和SqliteOpenHelper

This will give you access to your db via framework methods. 这将使您可以通过框架方法访问数据库。 It will enable you to use CursorLoader (automatic refresh on change), SyncAdapter (sync changes to a server), and some other features right out of the box. 它使您可以立即使用CursorLoader (更改时自动刷新), SyncAdapter (将更改同步到服务器)以及其他一些功能。 You will have to work with URI paths to identify your elements and post queries, since you have no direct access to the database. 由于您无权直接访问数据库,因此您将不得不使用URI路径来标识您的元素并发布查询。
This option is better documented, since it is an official android api, albeit you will have to do some reading before you can optimally use it. 由于它是官方的android api,因此可以更好地记录此选项,尽管您必须先阅读一些内容才能最佳使用它。


Also worth noting, if some other developer looks at your code, they will probably know how to use your ContentProvider (if you keep it consistent) but they will have to read into your source code (or documentation) to find out how to use your custom implementation. 同样值得注意的是,如果其他开发人员查看您的代码,他们可能会知道如何使用ContentProvider (如果保持一致),但是他们将不得不阅读源代码(或文档)以了解如何使用您的ContentProvider自定义实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM