【问题标题】:flutter_moor filter select query using more than one value inside whereflutter_moor 过滤选择查询在 where 中使用多个值
【发布时间】:2020-07-13 05:57:23
【问题描述】:

我正在尝试使用 Flutter 的 moor 包对我的数据库实施多值过滤器。

moor 已经有 where 方法,该方法接受一个表达式并将其转换为 sql 语句。喜欢:

 (select(exercisesTable)..where((e) => (e.name.equals(name)))).get(); 

但是由于多个值,我需要过滤数据。 在我搜索文档后,我发现了两种可能的解决方案:

  1. 使用CutomExpressionClasslink

    Expression expression = CustomExpression<bool, BoolType>(" water BETWEEN 4.0 AND 5.0 AND protein BETWEEN 4.0 AND 15.0 AND description LIKE CHESS%");
    

    但我收到此错误:*

    SqliteException:靠近“;”:语法错误,SQL逻辑错误

*

  1. 使用Custom select statementslink:
    我没有尝试过,因为我认为问题出在 sql 本身而不是 moor 包中。

【问题讨论】:

标签: sql sqlite flutter filter flutter-moor


【解决方案1】:

来自 SingleTableQueryMixin::where 方法的 cmets (link):

...      
/// If a where condition has already been set before, the resulting filter
/// will be the conjunction of both calls.
...

据此,您可以使用类似的东西:

 Future<List<TableData>> getTableList({int position, int type}) {
     final _select = select(table);
     if (position != null) {
        _select..where((tbl) => tbl.position.equals(position));
     }
     if (type != null) {
        _select..where((tbl) => tbl.type.equals(type));
     }
     return _select.get();
 }

【讨论】:

    【解决方案2】:

    您可以使用boolean algebra 功能来设置多个 where 条件。

    // find all animals that aren't mammals and have 4 legs
    select(animals)..where((a) => a.isMammal.not() & a.amountOfLegs.equals(4));
    
    // find all animals that are mammals or have 2 legs
    select(animals)..where((a) => a.isMammal | a.amountOfLegs.equals(2));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 2015-08-19
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多