4

How can I get an array with only the column "Name" of my Category model?

I can get all my categories using

List<Category> categoryList = new Select()
    .from(Category.class)
    .execute();

and then create another array with the name of the Category, but I guess there is a way to get it directly and I cannot find how.

jul
  • 36,404
  • 64
  • 191
  • 318
  • I've wondered the same thing for several months. I first thought `new Select("your_column")...execute();` would retrieve columns only but it doesn't. – RestInPeace Sep 25 '14 at 05:04
  • No. I created the array myself from the list of Category. – jul Feb 09 '15 at 21:05

1 Answers1

12

A little late in answering this, but the issue is because SQLiteUtils.rawQuery() makes the assumption that the Id column is always going to be in the cursor result. Set your columns String[] to {Id, your_column} and you'll be fine.

List<Category> categoryList = new Select(new String[]{"Id,your_column"}).from(Category.class).execute();
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
sjwoodr
  • 326
  • 3
  • 8
  • Note, your id column isn't necessarily `Id`. On Android, mine was `_id`, which is what the typical `ListView` expects. – Joshua Pinter Jun 25 '18 at 05:16
  • Also, if your query has any joins you'll need to specify the table names, otherwise you'll get an ambiguous column error. E.g. `new String[]{"categories.Id,categories.name"}` – Joshua Pinter Jun 25 '18 at 05:18