ContentProvider 的变量数据库名称

发布于 2024-11-27 01:40:15 字数 876 浏览 1 评论 0原文

我正在 Android 中创建一个自定义 ContentProvider,我找到的所有示例都显示数据库名称被硬编码,实例化如下:

public class ItemProvider extends ContentProvider {

    private static String DATABASE_NAME = "xyz";

    public static class ItemDatabaseHelper extends SQLiteOpenHelper {

    ItemDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

我想要做的是在运行时使用变量作为数据库名称,我不想硬编码数据库名称在课堂上。我尝试找到使用 ContentProvider 类执行此操作的示例,并浏览了文档。如果我放弃 ContentProvider 类并只使用 SQLiteOpenHelper 类,我就可以做到这一点,因为我可以将数据库名称作为参数传递给构造函数,但我无法确定 ContentProvider 是否可行。以下是我如何使用 SQLiteOpenHelper 获取数据库的变量名称:

public static class ItemDatabaseHelper extends SQLiteOpenHelper {

    ItemDatabaseHelper(Context context, String dbname) {
        super(context, dbname, null, DATABASE_VERSION);
    }

任何人都可以帮助我获取内容提供程序的变量数据库名称吗?

谢谢。

I am creating a custom ContentProvider in Android, all the examples I find show the database name being hardcoded, instantiated like this:

public class ItemProvider extends ContentProvider {

    private static String DATABASE_NAME = "xyz";

    public static class ItemDatabaseHelper extends SQLiteOpenHelper {

    ItemDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

What I want to do is use a variable for the database name at runtime, I dont want to hard code the database name in the class. I have tried to find examples of doing this using the ContentProvider class, and have looked through the documentation as well. I can do this if I shed the ContentProvider class and just use a SQLiteOpenHelper class because I can pass in the database name to the constructor as a parameter but I cannot figure out if its possible for ContentProvider. Here is how I get a variable name for the database using SQLiteOpenHelper:

public static class ItemDatabaseHelper extends SQLiteOpenHelper {

    ItemDatabaseHelper(Context context, String dbname) {
        super(context, dbname, null, DATABASE_VERSION);
    }

Can anyone help me get a variable database name for a Content Provider?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱已欠费 2024-12-04 01:40:15

我自己并没有真正使用过 ContentProviders,但是查看文档,我猜测问题的根源是您没有自己实例化 ContentProvider,而是 Android 在需要处理请求时会实例化 ContentProvider。

如果您希望应用程序在运行时定义数据库名称,那么您可以简单地使用公共静态变量,您可以从应用程序的另一部分适当地设置该变量。然后,您可以从您的 ContentProvider 引用此内容。

或者,如果您希望从 ContentProvider 请求某些内容的人能够指定要查询的数据库,那么从文档来看,您似乎可以使用请求 URI 的路径来指定要查询的数据库。

请求 URI 的结构详细信息如下: http://developer .android.com/guide/topics/providers/content-providers.html#urisum 并在该页面的更上方显示:

权限是识别提供者的,而不是路径;您的提供商可以按照您选择的任何方式解释 URI 的路径部分。

举个例子,我想你可以使用以下形式的 URI:

content://com.example.yourprovider/DB_REF/ID

然后在你的 ContentProvider 抽象方法的实现中 您可以解析 URI 来确定要使用的数据库。

警告一句 - 如果您打算使用这种方法,那么我建议不要使用直接传入的值。最好对有限列表使用某种验证,这样人们就不能只查询您的任何数据库(如果他们知道自己的名字)。

希望这是有道理的:)

I've not really used ContentProviders myself, but looking at the docs I'm guessing the root of the problem is that you don't instantiate the ContentProvider yourself, but that Android does when it is needed to handle a request.

If you want the database name to be defined at runtime by your application, then you could simply use a public static variable, which you set appropriately from another part of your application. You could then reference this from your ContentProvider.

Alternatively, if you're wanting the person who requests something from the ContentProvider to be able to specify the database to query, then from the docs it looks like you could use the path of the request URI to specify the database to query.

The structure of the request URI is detailed here: http://developer.android.com/guide/topics/providers/content-providers.html#urisum and further up this page it says:

The authority is what identifies the provider, not the path; your provider can interpret the path part of the URI in any way you choose.

So as an example, I would imagine you could use an URI of the form:

content://com.example.yourprovider/DB_REF/ID

Then in your implementation of the abstract methods of ContentProvider you could parse the URI to determine the DB to use.

One word of warning though - if you are going to use this approach, then I would suggest not using the value passed in directly. It would be much better to use some kind of validation against a finite list, so that people can't just query any of your databases (if they know their name).

Hopefully that makes sense :)

纵山崖 2024-12-04 01:40:15

首先,我要说的是,我在网上查看了很多 ContentProvider 代码的示例,最终对我帮助最大的是这个:

http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/

我的情况非常相似你的,我想要几个不同名称的数据库,其中包含车辆燃油里程数据。每个名称都由车辆名称(由用户指定)和当前年份组成,加上固定文本“fuel_data”以使数据库文件名易于阅读。

与我找到的所有示例不同,我不会在 ContentProvider 的 onCreate 方法中创建数据库实例。这是因为我当时还不知道车辆名称,这要归功于我的代码编写方式(以及 ContentProviders 在 Activity 生命周期的早期实例化的事实)。

一旦我知道了车辆名称(我可以轻松地构造数据库名称),我就调用添加到 ContentProvider 类中的一个简单的小方法。正如您所看到的,它所做的就是关闭先前的数据库(如果数据库已打开)并打开新数据库(使用我现在可用的名称)。

每次用户选择新车辆时,我都会再次调用此方法。

` 私有静态 FuelDatabase mDB = null;

 public static void switchDatabases( Context context, String newVehicleName )
 {
     if ( mDB != null )
     {
     mbB.close();
     }

     mDB = new FuelDatabase( context, newVehicleName + "." + getCurrentYear( context ) );
 }`

First, let me say that I've looked at lots of examples for ContentProvider code on the web, and the one that ended up helping me the most was this one:

http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/

I have a very similar situation to yours, where I want to have several differently-named databases containing vehicle fuel mileage data. Each of the names consists of a vehicle name (specified by the user) and the current year, plus the fixed text "fuel_data" to make the database filenames human-readable.

Unlike all of the examples I found, I do not create an instance of the database in the ContentProvider's onCreate method. This is because I don't yet know the vehicle name at that point, thanks to the way my code is written (and the fact that ContentProviders are instantiated so early in the Activity lifecycle).

Once I do know the vehicle name (from which I can easily construct the database name), I call a simple little method that I added to my ContentProvider class. As you can see, all it does is close the prior database (if one was open) and open the new one (using the name that I now have available).

I call this method again every time the user selects a new vehicle.

` private static FuelDatabase mDB = null;

 public static void switchDatabases( Context context, String newVehicleName )
 {
     if ( mDB != null )
     {
     mbB.close();
     }

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