2016-09-05 94 views
2

如何基于某些条件构建查询。查询构建器dbflow条件查询

我试着这样做

QueryBuilder builder = SQlite.Select().from(Table) 
    if(condition) { 
     builder.where(something) 
    } 
Cursor c = builder.query; 

,但它是不允许的。

我必须查询我的数据库的条件,我已经保存在首选项。我在搜索引擎中搜索到了各处,但无法找到一个示例。做这个功能是否存在于dbflow中如果是,那么如果没有任何其他orm(如greenDAO)如何使用此功能

回答

2

可以在DBFlow中创建条件查询。要查询表格的列,您必须将_Table附加到您的班级名称,然后访问其属性。这些_Table类是在构建期间生成的。

最简单的查询会是这样一个:

SQLite.select() 
     .from(YourTable.class) 
     .where(YourTable_Table.id.eq(someId) 
     .queryList(); 

您也可以通过在查询中使用.and.or添加新条件:

SQLite.select() 
     .from(YourTable.class) 
     .where(YourTable_Table.id.eq(someId) 
     .and(YourTable_Table.name.eq(someName) 
     .queryList(); 

对于一个更干净的代码,你也可以组条件进入如下条件组:

ConditionGroup conditionGroup = ConditionGroup.clause(); 
conditionGroup.and(YourTable_Table.id.eq(someId); 

if (someCondition) { 
    conditionGroup.and(YourTable_Table.name.eq(someName); 
} 

return SQLite.select() 
     .from(YourTable.class) 
     .where(conditionGroup) 
     .queryList(); 
+0

ConditionalGroup是我失踪了我的问题

1.从@trevjonez(特雷弗琼斯)

Where<SomeModel> query = SQLite.select() .from(SomeModel.class) .where(SomeModel_Table.date_field.greaterThan(someValue)); if(someCondition) { query = query.and(SomeModel_Table.other_field.eq(someOtherValue)); } else { query = query.and(SomeModel_Table.another_field.isNotNull()); } Cursor cursor = query.query(); //do stuff with cursor and close it — 

2.从@zshock两种方式... .thnks –

2

发现使用实现ConditionalGroup

ConditionGroup conditionGroup = ConditionGroup.clause(); 
conditionGroup.and(YourTable_Table.id.eq(someId); 

if (someCondition) { 
    conditionGroup.and(YourTable_Table.name.eq(someName); 
} 

return SQLite.select() 
     .from(YourTable.class) 
     .where(conditionGroup) 
     .queryList();