问题 如何使用Content Provider实现复杂查询?


我问这个是因为我不太清楚如何使用Android内容提供商。我有一个包含8个表的数据库的子集,我需要创建复杂的查询来获取一些数据。我的内容提供商可以很好地处理简单查询例如,我有一张桌子上的人物 PersonModel.java class和我使用以下方法获取数据:

String [] projection = {PersonModel.C_FIRST_NAME, PersonModel.C_LAST_NAME};
Cursor cursor = context.getContentResolver().query(
            MyProvider.CONTENT_URI_PERSONS, projection, null,
            null, null);

它完美无缺。

MYPROVIDER 我的每个表都有一堆CONTENT_URI常量。

public class MyProvider extends ContentProvider {
    MyDbHelper dbHelper;
    SQLiteDatabase db;


    private static final String AUTHORITY = "com.myapp.models";

    //Paths for each tables
    private static final String PATH_PROFILE_PICTURES = "profile_pictures";
    private static final String PATH_PERSONS = "persons";
    private static final String PATH_USERS = "users";
    ....

    //Content URIs for each table
    public static final Uri CONTENT_URI_PROFILE_PICTURES = Uri
        .parse("content://" + AUTHORITY + "/" + PATH_PROFILE_PICTURES);
    public static final Uri CONTENT_URI_PERSONS = Uri.parse("content://"
        + AUTHORITY + "/" + PATH_PERSONS);
    public static final Uri CONTENT_URI_USERS = Uri.parse("content://"
        + AUTHORITY + "/" + PATH_USERS);
    ...


    private static final int PROFILE_PICTURES = 1;
    private static final int PROFILE_PICTURE_ID = 2;
    private static final int PERSONS = 3;
    private static final int PERSON_ID = 4;
    private static final int USERS = 5;
    private static final int USER_ID = 6;

    private static final UriMatcher sURIMatcher = new UriMatcher(
        UriMatcher.NO_MATCH);
    static {
    sURIMatcher.addURI(AUTHORITY, PATH_PROFILE_PICTURES, PROFILE_PICTURES);
    sURIMatcher.addURI(AUTHORITY, PATH_PROFILE_PICTURES + "/#",
            PROFILE_PICTURE_ID);
    sURIMatcher.addURI(AUTHORITY, PATH_PERSONS, PERSONS);
    sURIMatcher.addURI(AUTHORITY, PATH_PERSONS + "/#", PERSON_ID);
    sURIMatcher.addURI(AUTHORITY, PATH_USERS, USERS);
    sURIMatcher.addURI(AUTHORITY, PATH_USERS + "/#", USER_ID);
    ...
    }



  public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {

    // Uisng SQLiteQueryBuilder instead of query() method
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    // check if the caller has requested a column which does not exists
    //checkColumns(projection);

    int uriType = sURIMatcher.match(uri);

    switch (uriType) {
    case PROFILE_PICTURES:
        queryBuilder.setTables(ProfilePictureModel.TABLE_PROFILE_PICTURE);
        break;
    case PROFILE_PICTURE_ID:
        // Adding the ID to the original query
        queryBuilder.appendWhere(ProfilePictureModel.C_ID + "="
                + uri.getLastPathSegment());
    case PERSONS:
        queryBuilder.setTables(PersonModel.TABLE_PERSON);
        break;
    case PERSON_ID:
        // Adding the ID to the original query
        queryBuilder.appendWhere(PersonModel.C_ID + "="
                + uri.getLastPathSegment());
    case USERS:
        queryBuilder.setTables(UserModel.TABLE_USER);
        break;
    case USER_ID:
        // Adding the ID to the original query
        queryBuilder.appendWhere(UserModel.C_ID + "="
                + uri.getLastPathSegment());

    default:
        throw new IllegalArgumentException("Unknown URI: " + uri);

    }

    db = dbHelper.getWritableDatabase();
    Cursor cursor = queryBuilder.query(db, projection, selection,
            selectionArgs, null, null, sortOrder);
    // make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

 }

这是我的内容提供商的一小部分。所以我的问题是:

1)如何在我的内容提供商中实现rawQuery()?或者我如何正确使用我的queryBuilder?,假设我想使用几个表执行此查询,重命名它们并将p1.id作为参数传递?

SELECT p1.first_name, p1_last_name 
FROM Person p1, Person P2, Relationship r 
WHERE p1.id = ? AND 
      p1.id = r.relative_id AND
      p2.id = r.related_id;

我试过这样做:在我的query()方法(如上所示)我有一个新的案例,名为GET_RELATIVES:

case GET_RELATIVES:
        db = dbHelper.getWritableDatabase();
        queryBuilder.setTables(PersonModel.TABLE_PERSON + " p1, "
                + PersonModel.TABLE_PERSON + " p2, "
                + RelationshipModel.TABLE_RELATIONSHIP + " r");
        queryBuilder.appendWhere("p2."+PersonModel.C_ID + "=" + uri.getLastPathSegment());
        queryBuilder.appendWhere("p2."+PersonModel.C_ID + "=" + "r.related_id");
        queryBuilder.appendWhere("p1."+PersonModel.C_ID + "=" + "r.relative_id");

所以我定义了一个新的PATH,CONTENT URI并将其添加到UriMatcher,如下所示:

private static final String PATH_GET_RELATIVES = "get_relatives";
public static final Uri CONTENT_URI_GET_RELATIVES = Uri
        .parse("content://" + AUTHORITY + "/"
                + PATH_GET_RELATIVES);
private static final int GET_RELATIVES = 22;

private static final UriMatcher sURIMatcher = new UriMatcher(
    UriMatcher.NO_MATCH);
static {
...
sURIMatcher.addURI(AUTHORITY, PATH_GET_RELATIVES, GET_RELATIVES);
}

但这似乎没有用,所以我想我可能在我的内容提供商或查询方法中定义了一些错误。

2)我不太确定为每个表提供一个名为TABLE_ID的常量并将其添加到switch-case的重点是什么。那用的是什么?我怎么称呼它?

希望有人能帮助我,谢谢!


9081
2017-08-08 18:51


起源



答案:


我实际上在最明显的地方找到了我的问题的答案:android文档。

第一个问题:实现rawQuery。它是这样的:

在我的内容提供程序中的switch-case内部,我添加了一个新的URI,对我来说是表之间的JOIN,所以我为它创建了一个新的ContentUri常量,一个新的ID,并在UriMatcher上注册它然后写了rawQuery。所以MyProvider现在看起来有点像这样:

public class MyProvider extends ContentProvider {
...
// JOIN paths
    private static final String PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = 
            "relationship_join_person_get_relatives";
...
public static final Uri CONTENT_URI_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = Uri
            .parse("content://" + AUTHORITY + "/"
                    + PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES);
...
    private static final int RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = 21;
private static final UriMatcher sURIMatcher = new UriMatcher(
            UriMatcher.NO_MATCH);
    static {
...
//JOINS
        sURIMatcher.addURI(AUTHORITY, PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES + "/#",
                RELATIONSHIP_JOIN_PERSON_GET_RELATIVES);
...

public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {

        // Uisng SQLiteQueryBuilder instead of query() method
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        // check if the caller has requested a column which does not exists
        //checkColumns(projection);

        int uriType = sURIMatcher.match(uri);

        switch (uriType) {
        ...
        case RELATIONSHIP_JOIN_PERSON_GET_RELATIVES:
            db = dbHelper.getWritableDatabase();
            String[] args = {String.valueOf(uri.getLastPathSegment())};
            Cursor cursor = db.rawQuery(
                    "SELECT p1.first_name, p1.last_name " +
                    "FROM Person p1, Person p2, Relationship r " +
                    "WHERE p1.id = r.relative_id AND " +
                    "p2.id = r.related_id AND " + 
                    "p2.id = ?", args);
            cursor.setNotificationUri(getContext().getContentResolver(), uri);
            return cursor;
        ...
}

并调用query()方法并将id ad传递给我在控制器中执行此操作的参数:

String[] projection = { PersonModel.C_FIRST_NAME,
                PersonModel.C_LAST_NAME };
        Cursor cursor = context.getContentResolver().query(
                ContentUris.withAppendedId(
                        AkdemiaProvider.CONTENT_URI_RELATIONSHIP_JOIN_PERSON_GET_RELATED, id), 
                        projection, null, null, null);

第二个问题:拥有TABLE_ID常量对于每个表传递一个id作为参数的查询很有用,我不知道如何调用传递这样的id的查询方法,这就是Android开发者文档如何解释如何这样做运用 ContentUris.withAppendedId

// Request a specific record.


Cursor managedCursor = managedQuery(
                ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
                projection,    // Which columns to return.
                null,          // WHERE clause.
                null,          // WHERE clause value substitution
                People.NAME + " ASC");   // Sort order.

我们想看看整个文档 这个链接。

希望这有助于其他任何有同样问题的人了解ContentProvider,ContentUris以及所有:)


14
2017-08-09 19:34



你太棒了,节省了我的一天,做了我的一天。 - Pratik Butani
@Carla Stabile如果我需要传递字符串来为多个具有相同字符串的表进行连接。 - Dilip Poudel


答案:


我实际上在最明显的地方找到了我的问题的答案:android文档。

第一个问题:实现rawQuery。它是这样的:

在我的内容提供程序中的switch-case内部,我添加了一个新的URI,对我来说是表之间的JOIN,所以我为它创建了一个新的ContentUri常量,一个新的ID,并在UriMatcher上注册它然后写了rawQuery。所以MyProvider现在看起来有点像这样:

public class MyProvider extends ContentProvider {
...
// JOIN paths
    private static final String PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = 
            "relationship_join_person_get_relatives";
...
public static final Uri CONTENT_URI_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = Uri
            .parse("content://" + AUTHORITY + "/"
                    + PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES);
...
    private static final int RELATIONSHIP_JOIN_PERSON_GET_RELATIVES = 21;
private static final UriMatcher sURIMatcher = new UriMatcher(
            UriMatcher.NO_MATCH);
    static {
...
//JOINS
        sURIMatcher.addURI(AUTHORITY, PATH_RELATIONSHIP_JOIN_PERSON_GET_RELATIVES + "/#",
                RELATIONSHIP_JOIN_PERSON_GET_RELATIVES);
...

public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {

        // Uisng SQLiteQueryBuilder instead of query() method
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        // check if the caller has requested a column which does not exists
        //checkColumns(projection);

        int uriType = sURIMatcher.match(uri);

        switch (uriType) {
        ...
        case RELATIONSHIP_JOIN_PERSON_GET_RELATIVES:
            db = dbHelper.getWritableDatabase();
            String[] args = {String.valueOf(uri.getLastPathSegment())};
            Cursor cursor = db.rawQuery(
                    "SELECT p1.first_name, p1.last_name " +
                    "FROM Person p1, Person p2, Relationship r " +
                    "WHERE p1.id = r.relative_id AND " +
                    "p2.id = r.related_id AND " + 
                    "p2.id = ?", args);
            cursor.setNotificationUri(getContext().getContentResolver(), uri);
            return cursor;
        ...
}

并调用query()方法并将id ad传递给我在控制器中执行此操作的参数:

String[] projection = { PersonModel.C_FIRST_NAME,
                PersonModel.C_LAST_NAME };
        Cursor cursor = context.getContentResolver().query(
                ContentUris.withAppendedId(
                        AkdemiaProvider.CONTENT_URI_RELATIONSHIP_JOIN_PERSON_GET_RELATED, id), 
                        projection, null, null, null);

第二个问题:拥有TABLE_ID常量对于每个表传递一个id作为参数的查询很有用,我不知道如何调用传递这样的id的查询方法,这就是Android开发者文档如何解释如何这样做运用 ContentUris.withAppendedId

// Request a specific record.


Cursor managedCursor = managedQuery(
                ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
                projection,    // Which columns to return.
                null,          // WHERE clause.
                null,          // WHERE clause value substitution
                People.NAME + " ASC");   // Sort order.

我们想看看整个文档 这个链接。

希望这有助于其他任何有同样问题的人了解ContentProvider,ContentUris以及所有:)


14
2017-08-09 19:34



你太棒了,节省了我的一天,做了我的一天。 - Pratik Butani
@Carla Stabile如果我需要传递字符串来为多个具有相同字符串的表进行连接。 - Dilip Poudel


下面的代码对我有用。在应用程序的内容提供者中:

public static final String PATH_JOIN_TWO_TABLES = "my_path";

    public static final Uri URI_JOIN_TWO_TABLES =
            Uri.parse("content://" + AUTHORITY + "/" + PATH_JOIN_TWO_TABLES);

    private static final int ID_JOIN_TWO_TABLES = 1001;

    private static final UriMatcher sURIMatcher = new UriMatcher(
            UriMatcher.NO_MATCH);

    static {
        sURIMatcher.addURI(AUTHORITY,
                PATH_JOIN_TWO_TABLES + "/#", ID_JOIN_TWO_TABLES );
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection,String[] selectionArgs,
                        String sortOrder, CancellationSignal cancellationSignal) {

        int uriType = sURIMatcher.match(uri);
            switch (uriType) {

                case ID_JOIN_TWO_TABLES:
                    return getWritableDatabase()
                            .rawQuery("select * from " +
                                    "table_one" + " LEFT OUTER JOIN "
                                    + "table_two" + " ON ("
                                    + "table_one.ID"
                                    + " = " + "table_two.id" + ")", null);
            }
        return super.query(uri, projection, selection, selectionArgs, sortOrder, cancellationSignal);
    }

在您的Activity或Fragment中进行查询时:

 Cursor cursor = getActivity().getContentResolver()
                .query(ContentUris.withAppendedId(MYContentProvider.URI_JOIN_TWO_TABLES, MyContentProvider.ID_JOIN_TWO_TABLES), null, null, null, null);

希望对你有效。


2
2018-04-29 05:03



我没有看到你的回答和2014年8月9日的OP之间的区别 stackoverflow.com/a/25222294/1558016  我错过了什么吗? - OYRM


对于简单查询,请在ContentProvider中使用selectionArgs。它的工作原理如下

String[] args = { "first string", "second@string.com" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);

在其中包含TABLE_ID,为每个表创建不同的查询。

请参阅以下类,了解内容提供商中的所有多个表

  1. Vogella教程1
  2. Vogella教程2
  3. 使用Android中的内容提供程序公开多个表的最佳做法

-3
2017-08-08 19:03



谢谢你的回答。我实际上做了Vogella教程来制作我的内容提供者,但如果你看到那里没有复杂的查询,这是我的主要问题。你知道怎么做吗? - Carla Stabile
它具有大多数需要的查询代码。复杂查询是您的业务逻辑。为此,请查看上面提到的selectionArgs的示例或查找SqliteQueryBuilder的示例。这个链接应该有帮助 programcreek.com/java-api-examples/...。如果有帮助,请接受答案。 - AnkitSomani