Realm-java: Problem In Query Writing

Created on 1 Sep 2016  ·  3Comments  ·  Source: realm/realm-java

i have 4 field in realm object
3 of 4 field can have only 4 value
when i want write a query for this table
for example:
@PrimaryKey
private int ID ;
private int FKind;
private int StrhCode;
private int Status;

in query's if this 3 fileds values equal -1 no effect in result but >1 have effect in result
i write a function:

//pesucode
if(id>1)
{
      return one row that ID=id
}
else
{
       return fKind,strhCode,status Conditions; -> many rows;
}
//ActualCode
public RealmResult<FactorHead> getFactors(int id,int fKind,int strhCode,int status)
{
          if(id>0)
          {
                  return realm.where(FactorHead.Class).equal("ID",id).findFirst();//this is true
          }
          else{
                  ????????????????????????????????????
                  //I want  have multi Conditions for  fKind,strhCode,status if any field<0
                  //no effect in result  if >0 effect i result
          }

}
//in sqlite or other string query language support
str="select * from FactorHead where ";
if(status>0)
{
   str=str+" and Status="+status;
}
if(fKind>0)
{
   str=str+" and Fkind="+fKind;
}
if(strhCode>0)
{
   str=str+" and StrhCode="+strhCode;
}

this is my question?
how build this queries with these Conditions one filed be or not be
n! state????? for writng query with n field ???

T-Help

Most helpful comment

RealmQuery query = realm.where(FactorHead.class);
if (status > 0) {
    query = query.equalTo("Status",status);
}
if (fKind > 0) {
    query = query.equalTo("FKind", fKind);
}
if (strhCode > 0) {
    query = query.equalTo("StrhCode", strhCode);
}
return query.findAll();

All 3 comments

Please fix your formatting by wrapping your code in the following tags:

Enter code here

Currently I can't really read it.

RealmQuery query = realm.where(FactorHead.class);
if (status > 0) {
    query = query.equalTo("Status",status);
}
if (fKind > 0) {
    query = query.equalTo("FKind", fKind);
}
if (strhCode > 0) {
    query = query.equalTo("StrhCode", strhCode);
}
return query.findAll();

Thanks thanks thanks
that is right
Realm For me in new but great to high speed develop app

Was this page helpful?
0 / 5 - 0 ratings