9

I am having two tables with 1:1 relationship, I am using content provider and cursorloader.

How would I make a join query to work with cursor loader? I could hack it up somehow with rawSql inside content provider but how to do it in cursor loader constructor is beyond me.

Thanks a lot !

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 + "));";
urSus
  • 12,492
  • 12
  • 69
  • 89
  • I'm pretty sure you're going to need to write raw SQL to do joins. – Falmarri Dec 28 '12 at 22:37
  • how would I write a raw SQL query when creating Cursor Loader? It only accepts those strings as parameters (projection, selection, etc) http://developer.android.com/reference/android/content/CursorLoader.html – urSus Dec 29 '12 at 04:02

3 Answers3

10

Actually, you don't have to use a custom task loader. In a nutshell, one solution is to create a Uri "content://AUTHORITY/TableArticlesWithDetail". Then in your content provider, check for that Uri and do a raw SQL to do the join.

See how to use join query in CursorLoader when its constructor does not support it for details.

Community
  • 1
  • 1
U Avalos
  • 6,538
  • 7
  • 48
  • 81
2

Easiest solution is to create a view that joins your tables and access the view from your CursorLoader using a Uri.

zyamys
  • 1,609
  • 1
  • 21
  • 23
0
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);

Do like This