在 2 个 Android 应用程序之间共享 SQLite 数据库?
我需要在两个应用程序之间共享一个数据库。我知道数据库将在 /data/data/MY_PACKAGE/databases/ 上创建。由于包名称不同,当我在任一应用程序上创建数据库时,是否可以定义一个包名称的路径? 谢谢。
I need to share a single database between 2 apps. I know that the database will be created on /data/data/MY_PACKAGE/databases/ . Since the packages names are different is it possible to define the path to one package name when I create the database on either app?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:下面描述的方法依赖于
android:sharedUserId
,从 API 级别 29 (Android 10) 开始已弃用。您当然可以在两个应用程序之间共享一个数据库。
为了在应用程序之间共享数据(前提是它们由同一发布者发布),您需要在两个应用程序的 AndroidManifest.xml 中指定共享用户 ID。
(它没有记录,但共享用户 ID 需要是一个至少带有一个点分隔符的字符串)
其余的很简单,您不需要弄乱数据库路径。只需在两个应用程序中使用相同的 DBAdapter 即可。在托管数据库的应用程序中,使用本机上下文调用 DBAdapter。
在第二个应用程序中,使用数据库托管应用程序的上下文访问数据库。
首先,定义共享上下文:
然后使用共享上下文打开 DBAdapter:
最后一点,如果您的数据库在清单中设置共享用户 ID 之前就已存在,则您将需要在物理设备上卸载/重新安装应用程序,以免您将自己锁定在数据库之外(sqlite 错误 14)。另一方面,模拟器可能会更加宽容。最重要的是,如果您的应用程序发布在 Android 市场上,事后设置共享用户 ID 是行不通的。
希望这有帮助。
UPDATE: The method described below relies on
android:sharedUserId
, deprecated as of API level 29 (Android 10).You certainly can share a single database between 2 apps.
In order to share data between apps (provided they are issued by the same publisher) you will need to specify a shared user id in the AndroidManifest.xml of both apps.
(It's undocumented, but the shared user id needs to be a string with at least one dot separator)
The rest is easy, and you don't need to mess around with the database path. Just use the same DBAdapter in both apps. In the app that hosts the database, call the DBAdapter with the native context.
In the second app, access the database with the context of the database hosting app.
First, define the shared context:
Then open the DBAdapter with the shared context:
As a final note, if your database exists previous to setting the shared user id in the manifest, you will need to uninstall/reinstall the apps on a physical device, lest you will lock yourself out of your database (sqlite error 14). The emulator, on the other hand, might prove to be more forgiving. Bottom line, if your apps are published on the Android market, setting a shared user id in an afterthought will not work.
Hope this helps.
数据库路径对于每个应用程序都是私有的,据我所知,不可能跨应用程序直接访问它。
然而,一种方法是一个应用程序使用 使其数据库可供另一个应用程序访问内容提供者。看看这是否适合你。
The database path is private for each application and as far as i know it's not possible to access it directly across applications.
However one approach is that one application makes it's database accessible to the other one using a ContentProvider. Check out if that works for you.
只要您在两个应用程序上使用相同的证书,您的应用程序就会在同一进程上运行,并且表现为同一应用程序
检查android文档的这一部分
http://developer.android.com/tools/publishing/app-signing .html#策略
As long as you use the same certificate on both applications, your applications will run on same process and behave as being same application
check this section of android documentation
http://developer.android.com/tools/publishing/app-signing.html#strategies