vendredi 27 novembre 2015

How to get datas from android cursor in a belongs to many relationship?

I have a sites table where each site can belongs to many categories and a category can have many sites.

So the raw query should be that one:

cursor = db.rawQuery(
    "SELECT " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_ID + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_REMOTE_ID + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_NAME + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_DIRECTORY_NAME + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_MODIFIED + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_TYPE + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_LONGITUDE + ", " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_LATITUDE + ", " +
     SitesDBOpenHelper.DATABASE_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_ID + " " +
     SitesDBOpenHelper.DATABASE_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_CODE + " " +
     SitesDBOpenHelper.DATABASE_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_NAME + " " +
     " FROM " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE +
     " INNER JOIN " +
     SitesDBOpenHelper.DATABASE_SITES_CATEGORIES_TABLE + " ON " +
     SitesDBOpenHelper.DATABASE_SITES_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_SITE_ID + " = " +
     SitesDBOpenHelper.DATABASE_SITES_TABLE + "." + SitesDBOpenHelper.KEY_ID +
     " INNER JOIN " +
     SitesDBOpenHelper.DATABASE_CATEGORIES_TABLE + " ON " +
     SitesDBOpenHelper.DATABASE_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_ID + " = " +
     SitesDBOpenHelper.DATABASE_SITES_CATEGORIES_TABLE + "." + SitesDBOpenHelper.KEY_CATEGORY_ID +
     " WHERE " +
     SitesDBOpenHelper.KEY_LATITUDE
     + " > ? AND "
     + SitesDBOpenHelper.KEY_LONGITUDE
     + " > ? AND "
     + SitesDBOpenHelper.KEY_LATITUDE
     + " < ? AND "
     + SitesDBOpenHelper.KEY_LONGITUDE
     + " < ? ",
     new String[]{Double.toString(bounds[0]), Double.toString(bounds[1]), Double.toString(bounds[2]), Double.toString(bounds[3])}
     );

Now my question is : How to access from these datas from a cursor because typically, each site is supposed to get an array of categories it belongs to.

Could you please give me an example?

Regards

Aucun commentaire:

Enregistrer un commentaire