,用于使用内容提供商Android的多个表的最佳实践多个、提供商、内容、Android

2023-09-12 06:51:27 作者:我怕我们越来越远

我要建一个应用程序,我对事件表和场地的表。我希望能够授予其他应用程序访问这些数据。我有相关的最佳实践这种问题的几个问题。

I'm building an app where I have a table for events and a table for venues. I want to be able to grant other applications access to this data. I have a few questions related to best practices for this kind of problem.

我应该如何构建数据库类? 我现在有类EventsDbAdapter和VenuesDbAdapter,这对于每个查询表提供的逻辑,同时还有一个独立DbManager(扩展SQLiteOpenHelper),用于管理数据库版本,创建/升级数据库,使访问数据库(getWriteable / ReadeableDatabase)。这是推荐的解决方案,否则我会过得更好或者巩固一切一个类(即在DbManager),或分离的一切,让每个适配器扩展SQLiteOpenHelper?

How should I structure the database classes? I currently have classes for EventsDbAdapter and VenuesDbAdapter, which provide the logic for querying each table, while having a separate DbManager (extends SQLiteOpenHelper) for managing database versions, creating/upgrading databases, giving access to database (getWriteable/ReadeableDatabase). Is this the recommended solution, or would I be better off either consolidating everything to one class (ie. the DbManager) or separation everything and letting each Adapter extends SQLiteOpenHelper?

我应该如何设计内容提供商的多个表? 扩展previous的问题,我应该使用一个内容提供者为整个应用程序,或者我应该创建单独的供应商为活动和场地?

How should I design content providers for multiple tables? Extending the previous question, should I use one Content Provider for the whole app, or should I create separate providers for Events and Venues?

大多数的例子,我觉得只能处理一个表的应用程序,所以我会AP preciate这里的指针。

Most examples I find only deal with single table apps, so I would appreciate any pointers here.

推荐答案

这也许太晚了一点,但其他人可能会发现这很有用。

It's probably a bit late for you, but others may find this useful.

首先,您需要创建多个CONTENT_URIs

First you need to create multiple CONTENT_URIs

public static final Uri CONTENT_URI1 = 
    Uri.parse("content://"+ PROVIDER_NAME + "/sampleuri1");
public static final Uri CONTENT_URI2 = 
    Uri.parse("content://"+ PROVIDER_NAME + "/sampleuri2");

然后你扩大你的URI匹配器

Then you expand your URI Matcher

private static final UriMatcher uriMatcher;
static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri1", SAMPLE1);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri1/#", SAMPLE1_ID);      
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri2", SAMPLE2);
    uriMatcher.addURI(PROVIDER_NAME, "sampleuri2/#", SAMPLE2_ID);      
}

然后创建表

private static final String DATABASE_NAME = "sample.db";
private static final String DATABASE_TABLE1 = "sample1";
private static final String DATABASE_TABLE2 = "sample2";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE1 =
    "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE1 + 
    " (" + _ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "data text, stuff text);";
private static final String DATABASE_CREATE2 =
    "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE2 + 
    " (" + _ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "data text, stuff text);";

不要忘了第二个 DATABASE_CREATE 添加到的onCreate()

您要使用的的switch-case 的块,以确定哪些表被使用。这是我的插入code

You are going to use a switch-case block to determine what table is used. This is my insert code

@Override
public Uri insert(Uri uri, ContentValues values) {
    Uri _uri = null;
    switch (uriMatcher.match(uri)){
    case SAMPLE1:
        long _ID1 = db.insert(DATABASE_TABLE1, "", values);
        //---if added successfully---
        if (_ID1 > 0) {
            _uri = ContentUris.withAppendedId(CONTENT_URI1, _ID1);
            getContext().getContentResolver().notifyChange(_uri, null);    
        }
        break;
    case SAMPLE2:
        long _ID2 = db.insert(DATABASE_TABLE2, "", values);
        //---if added successfully---
        if (_ID2 > 0) {
            _uri = ContentUris.withAppendedId(CONTENT_URI2, _ID2);
            getContext().getContentResolver().notifyChange(_uri, null);    
        }
        break;
    default: throw new SQLException("Failed to insert row into " + uri);
    }
    return _uri;                
}

您需要devide了删除更新的getType 等,无论您的供应商要求DATABASE_TABLE或CONTENT_URI您将添加一个案例,有DATABASE_TABLE1或CONTENT_URI1于一体,#2,未来等了你想要的。

You will need to devide up the delete, update, getType, etc. Wherever your provider calls for DATABASE_TABLE or CONTENT_URI you will add a case and have DATABASE_TABLE1 or CONTENT_URI1 in one and #2 in the next and so on for as many as you want.