7

I am trying to query my database for all models with primary keys in a list. This is my query (idsList is an ArrayList containing integers):

new Select().from(PostModel.class)
    .where(Condition.column(PostModel$Table.ID).in(0, idsList))
    .async().queryList(listener);

But Android Studio highlights the where condition, stating

"Cannot resolve method 'where(com.raizlabs.android.dbflow.sql.builder.Condition.In)"

So is Condition.In not considered a condition? How can I query all Models with primaryKey in an ArrayList?

I am using DBFlow 2.0. I can also use a regular SQL query String as a substitute, but I am not that versed in SQL, so if you could provide an SQL query String for my problem, that would be a possible workaround.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51

5 Answers5

9

DBFlow v3.x now allows you to pass a collection to Condition.in()

List<String> ids = new ArrayList<>();

Condition.In in = Condition.column(Tree_Table.ID.getNameAlias()).in(ids);

long count = new Select().count().from(Tree.class)
        .where(in)
        .count();
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
6

Create a In Condition:

List<String> ids = new ArrayList<String>();

Condition.In in = Condition.column(Tree$Table.ID).in(ids.get(0));
for (i = 1; i < ids.size(); i++){
      in.and(ids.get(i));
}

long count = new Select().count().from(Tree.class)
        .where(in)
        .count();
tread
  • 10,133
  • 17
  • 95
  • 170
1

Looking at the DBFlow documentation, the IN statement is modelled so that if your idsList contained ["Test", "Test2", "TestN"] then your code would need to be:

Condition.column(MyTable$Table.NAME).in("Test").and("Test2").and("TestN")

...so it looks like you'll need to enumerate each item in the array.

The regular SQL would be something like:

select *
from PostModel
where ID in ('Test', 'Test2', 'TestN')

...but that still means you need to enumerate each item in the array.

TobyLL
  • 2,098
  • 2
  • 17
  • 23
  • Thanks, this is weird, because .in() accepts multiple parameters. Also it will not help me much, because my list of ids might contain about 100 entries, thanks for checking it out though! – A. Steenbergen May 12 '15 at 20:36
1

For those on DBFlow 4.x, this is now even more easier to do:

SQLite.select()
    .from(PostModel.class)
    .where(PostModel_Table.ID.in(idsList))
    .async()
    .queryList(listener);

Just use the in(...) keyword together with the list of values within your where(...) call.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
-1

I had the same problem with Condition.In

What I did was something like this:

 Condition.In in = Condition.column(PostModel$Table.ID).in(0, idsList);
 ConditionQueryBuilder<PostModel> conditionQueryBuilder = new ConditionQueryBuilder<>(PostModel.class, in);

 List<PostModel> posts = new Select().from(PostModel.class).where(conditionQueryBuilder).queryList();

This is done with your idsList behind the scenes:

Collections.addAll(this.inArguments, idsList);

Hope this helps.

D.D.
  • 154
  • 1
  • 5