查询生成器 dbflow 条件查询
Query builder dbflow conditional query
如何根据特定条件构建查询。
我试过这样做
QueryBuilder builder = SQlite.Select().from(Table)
if(condition) {
builder.where(something)
}
Cursor c = builder.query;
但这是不允许的。
我必须根据我在首选项中保存的条件查询我的数据库。我在 thr 文档中到处搜索,但找不到一个示例。 dbflow 中是否存在此功能如果是,那么如果没有其他任何具有此功能的 orm(如 greenDAO)如何
您可以在 DBFlow 中创建条件查询。要查询 table 的列,您必须将 _Table
附加到您的 class 名称,然后访问它的 属性。这些 _Table
classes 是在构建期间生成的。
最简单的查询是这个:
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();
找到了解决我的问题的两种方法
1.from @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.from @zshock 使用 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();
如何根据特定条件构建查询。
我试过这样做
QueryBuilder builder = SQlite.Select().from(Table)
if(condition) {
builder.where(something)
}
Cursor c = builder.query;
但这是不允许的。
我必须根据我在首选项中保存的条件查询我的数据库。我在 thr 文档中到处搜索,但找不到一个示例。 dbflow 中是否存在此功能如果是,那么如果没有其他任何具有此功能的 orm(如 greenDAO)如何
您可以在 DBFlow 中创建条件查询。要查询 table 的列,您必须将 _Table
附加到您的 class 名称,然后访问它的 属性。这些 _Table
classes 是在构建期间生成的。
最简单的查询是这个:
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();
找到了解决我的问题的两种方法
1.from @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.from @zshock 使用 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();