在 2 个 Android 应用程序之间共享 SQLite 数据库?

发布于 2024-11-29 17:31:14 字数 117 浏览 0 评论 0原文

我需要在两个应用程序之间共享一个数据库。我知道数据库将在 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-12-06 17:31:14

更新:下面描述的方法依赖于 android:sharedUserId,从 API 级别 29 (Android 10) 开始已弃用。

您当然可以在两个应用程序之间共享一个数据库。

为了在应用程序之间共享数据(前提是它们由同一发布者发布),您需要在两个应用程序的 AndroidManifest.xml 中指定共享用户 ID。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="my.app" ... >

(它没有记录,但共享用户 ID 需要是一个至少带有一个点分隔符的字符串)

其余的很简单,您不需要弄乱数据库路径。只需在两个应用程序中使用相同的 DBAdapter 即可。在托管数据库的应用程序中,使用本机上下文调用 DBAdapter。

DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();

在第二个应用程序中,使用数据库托管应用程序的上下文访问数据库。
首先,定义共享上下文:

Context sharedContext = null;
    try {
        sharedContext = this.createPackageContext("replace.with.host.package.name", Context.CONTEXT_INCLUDE_CODE);
        if (sharedContext == null) {
            return;
        }
    } catch (Exception e) {
        String error = e.getMessage(); 
        return;
        }   

然后使用共享上下文打开 DBAdapter:

DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
sharedDBadapter.open();

最后一点,如果您的数据库在清单中设置共享用户 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.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="my.app" ... >

(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.

DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();

In the second app, access the database with the context of the database hosting app.
First, define the shared context:

Context sharedContext = null;
    try {
        sharedContext = this.createPackageContext("replace.with.host.package.name", Context.CONTEXT_INCLUDE_CODE);
        if (sharedContext == null) {
            return;
        }
    } catch (Exception e) {
        String error = e.getMessage(); 
        return;
        }   

Then open the DBAdapter with the shared context:

DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
sharedDBadapter.open();

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.

拔了角的鹿 2024-12-06 17:31:14

数据库路径对于每个应用程序都是私有的,据我所知,不可能跨应用程序直接访问它。

然而,一种方法是一个应用程序使用 使其数据库可供另一个应用程序访问内容提供者。看看这是否适合你。

内容提供商存储和检索数据并使其可访问
所有应用程序。它们是跨区域共享数据的唯一方式
应用程序;所有 Android 都没有共同的存储区域
可以访问。

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.

Content providers store and retrieve data and make it accessible to
all applications. They're the only way to share data across
applications; there's no common storage area that all Android packages
can access.

青萝楚歌 2024-12-06 17:31:14

只要您在两个应用程序上使用相同的证书,您的应用程序就会在同一进程上运行,并且表现为同一应用程序
检查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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文