DBFlow select where is COLUMN in the list?

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

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

But Android Studio highlights the conditions in which it states

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

So, the condition. Not considered a condition? How can I request all models with primaryKey in an ArrayList?

I am using DBFlow 2.0. I can also use the regular SQL String query as a replacement, but I'm not so good at SQL, so if you could provide a SQL String query for my problem, this would be a possible workaround.

+6
source share
4 answers

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(); 
+5
source

Create 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(); 
+5
source

Looking at the DBFlow Documentation , the IN statement is modeled so that if your idsList contains ["Test", "Test2", "TestN"] then your code should be:

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

... so you need to list each element in the array.

Normal SQL will look something like this:

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

... but it still means that you need to list each element in the array.

+1
source

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 list behind the scenes:

 Collections.addAll(this.inArguments, idsList); 

Hope this helps.

-1
source

Source: https://habr.com/ru/post/987066/


All Articles