本文介绍了在 Android 中使用内容提供程序公开多个表的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中有一张活动桌和一张场地桌.我希望能够授予其他应用程序访问此数据的权限.我有几个与此类问题的最佳做法相关的问题.

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.

  1. 我应该如何构建数据库类?我目前有 EventsDbAdapter 和 VenuesDbAdapter 类,它们提供查询每个表的逻辑,同时有一个单独的 DbManager(扩展 SQLiteOpenHelper)用于管理数据库版本、创建/升级数据库、提供对数据库的访问(getWriteable/ReadeableDatabase).这是推荐的解决方案,还是我最好将所有内容合并到一个类(即 DbManager)或分离所有内容并让每个适配器扩展 SQLiteOpenHelper?

  1. 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?

我应该如何为多个表格设计内容提供程序?扩展上一个问题,我应该为整个应用程序使用一个 Content Provider,还是应该为 Events 和 Venues 创建单独的提供程序?

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?

我发现的大多数示例只处理单表应用程序,所以我很感激这里的任何提示.

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

Don't forget to add the second DATABASE_CREATE to onCreate()

您将使用 switch-case 块来确定使用的表.这是我的插入代码

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;                
}

您需要划分 deleteupdategetType 等.无论您的提供者在哪里调用 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.

这篇关于在 Android 中使用内容提供程序公开多个表的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:32