将Room数据库公开给其他应用 [英] Exposing a Room database to other apps
问题描述
Android Architectural组件引入了新概念来在本地存储应用程序数据:房间.
Android Architectural components introduced new concept to store application data locally: Room.
以前,我们使用ContentProvider
可以将数据库公开给其他应用程序.我们如何对Room做同样的事情?
Previously using a ContentProvider
we can expose a database to other applications. How to we do the same with Room?
推荐答案
当我们使用Room创建数据库时,如何向其他应用程序公开数据库?
How to expose database to other apps when we create database using Room?
最可能的答案:使用ContentProvider
.最大的区别是您将使用您的RoomDatabase
及其getOpenHelper()
方法,而不是自己使用SQLiteOpenHelper
.您将使用与以前相同的方法来进行查询,插入,更新和删除,尽管SupportSQLiteDatabase
的API与SQLiteDatabase
的API略有不同.
The most likely answer: use a ContentProvider
. The biggest difference is that you will use your RoomDatabase
and its getOpenHelper()
method, rather than working with SQLiteOpenHelper
yourself. You would use the same sort of methods for queries, inserts, updates, and deletes as before, though SupportSQLiteDatabase
has a slightly different API than does SQLiteDatabase
.
并没有阻止您使ContentProvider
使用@Dao
方法取回对象的方法,但是您必须转过来将其转换回Cursor
对象(对于query()
而言),这可能很乏味.
There is nothing stopping you from having your ContentProvider
use @Dao
methods to get objects back, but then you have to turn around and convert them back into Cursor
objects (in the case of query()
), and this may be tedious.
与非Room数据库一样,也欢迎您通过任何其他适合您需求的IPC机制来公开Room数据库,例如具有AIDL定义接口的绑定服务. Room与这些IPC机制没有直接关系,但SQLiteDatabase
也不如此.
As with non-Room databases, you are also welcome to expose Room databases through any other IPC mechanism that suits your needs, such as a bound service with an AIDL-defined interface. Room has no direct relationship with those IPC mechanisms, but neither does SQLiteDatabase
.
这篇关于将Room数据库公开给其他应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!