简体   繁体   中英

Android Sqlite with ContentProvider or SQLiteOpenHelper

I was working around with android db and met 2 methods of working:

1) Using just SqliteOpenHelper

2) Using ContentProvider and SqliteOpenHelper

Is there any advantage using one or other ?

This question is primarily opinion based, but to sum it up:

1) Using just SqliteOpenHelper

You will have to manage the db access yourself. Provide methods for CRUD access and have full control. Updates can and should still be handled with SqliteOpenHelper .

2) Using ContentProvider and 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. You will have to work with URI paths to identify your elements and post queries, since you have no direct access to the database.
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.


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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