【问题标题】:CursorLoader - Two tables join query?CursorLoader - 两个表连接查询?
【发布时间】:2012-12-28 22:24:06
【问题描述】:

我有两个具有 1:1 关系的表,我正在使用内容提供程序和 cursorloader。

如何使用游标加载器进行连接查询?我可以在内容提供程序中使用 rawSql 以某种方式破解它,但如何在游标加载器构造函数中执行此操作超出了我的范围。

非常感谢!

private static final String CREATE_TABLE_ARTICLES = "create table "
            + TABLE_ARTICLES + "("
            + COLUMN_ARTICLE_ID + " integer primary key autoincrement, "
            + COLUMN_URL + " text not null unique, "
            + COLUMN_TITLE + " text not null, "
            + COLUMN_PRICE + " text not null, "
            + COLUMN_ADDED + " text not null, "
            + COLUMN_IMG_URL + " text);";

    private static final String CREATE_TABLE_ARTICLE_DETAIL = "create table "
            + TABLE_ARTICLE_DETAILS + "("
            + COLUMN_ARTICLE_DETAIL_ID + " integer primary key autoincrement, "
            + COLUMN_DESC + " text not null, "
            + COLUMN_LOCALITY + " text, "
            + COLUMN_TYPE + " text not null, "
            + COLUMN_SELLER + " text not null, "
            + COLUMN_SELLER_PHONE + " text, "
            + COLUMN_IMAGE_COUNT + " integer default 0, "
            + COLUMN_ARTICLE + " integer, foreign key (" + COLUMN_ARTICLE + ") references " + TABLE_ARTICLES + "(" + COLUMN_ARTICLE_ID + "));";

【问题讨论】:

标签: android join android-contentprovider android-cursorloader


【解决方案1】:

实际上,您不必使用自定义任务加载器。 简而言之,一种解决方案是创建一个 Uri“content://AUTHORITY/TableArticlesWithDetail”。 然后在您的内容提供程序中,检查该 Uri 并执行原始 SQL 来执行连接。

详情请参阅how to use join query in CursorLoader when its constructor does not support it。

【讨论】:

    【解决方案2】:

    最简单的解决方案是创建一个视图来连接您的表并使用 Uri 从您的 CursorLoader 访问该视图。

    【讨论】:

      【解决方案3】:
      Cursor c = db.query(
          RefuelTable.TABLE_NAME + " , " + ExpenseTable.TABLE_NAME,
          Utils.concat(RefuelTable.PROJECTION, ExpenseTable.PROJECTION),
           RefuelTable.EXP_ID + " = " + ExpenseTable.ID + " AND  "  +    RuelTable.ID + " = " +  id ,
          null,
          null,
          null,
          null);
      

      这样做

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-07
        • 2016-05-27
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-05
        相关资源
        最近更新 更多