DBFlow select where COLUMN in List?
Asked Answered
D

5

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.

Domenicadomenico answered 12/5, 2015 at 15:1 Comment(3)
Have you sorted this out? Please post your answer.Latrell
I didn't, but I will look into your answerDomenicadomenico
You marked the answer that was 1 year later than me, with the same answer as correct. Dissapointing.Latrell
F
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();
Freon answered 30/9, 2016 at 16:55 Comment(1)
i rather use StringQueryEan
L
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();
Latrell answered 30/7, 2015 at 9:39 Comment(0)
S
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.

Santosantonica answered 12/5, 2015 at 16:18 Comment(1)
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!Domenicadomenico
R
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.

Respectively answered 10/10, 2018 at 12:11 Comment(0)
C
-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.

Clarkclarke answered 10/7, 2015 at 8:10 Comment(1)
This creates raw SQL: WHERE ID` IN (0,'[1, 2, 3, 4, 5]')"Latrell

© 2022 - 2024 — McMap. All rights reserved.