【问题标题】:How to write customSelect query using Flutter moor/drift如何使用 Flutter moor/drift 编写 customSelect 查询
【发布时间】:2022-01-05 23:52:00
【问题描述】:

我坚持使用 moor 使用变量的自定义查询。使用时不返回列表 SELECT * FROM books WHERE title LIKE searchString;。我错过了什么吗?

代码:

Stream<List<Book>> getFilteredBook(String searchString) {
    searchString = 'the';
    return customSelect(
      //query works fine
      //'SELECT * FROM books;',
      'SELECT * FROM books WHERE title LIKE searchString;',
      variables: [
        Variable.withString(searchString),
      ],
      readsFrom: {books},
    ).watch().map((rows) {
      // Turning the data of a row into a Book object
      return rows.map((row) => Book.fromData(row.data)).toList();
    });
  }

【问题讨论】:

    标签: flutter sqlite moor


    【解决方案1】:

    您可以使用问号 (?) 作为变量的占位符:

      Stream<List<Book>> getFilteredBook(String searchString) {
        searchString = 'the';
        return customSelect(
          //query works fine
          //'SELECT * FROM books;',
          'SELECT * FROM books WHERE title LIKE ?;',
          variables: [
            Variable.withString(searchString),
          ],
          readsFrom: {books},
        ).watch().map((rows) {
          // Turning the data of a row into a Book object
          return rows.map((row) => Book.fromData(row.data)).toList();
        });
      }
    

    【讨论】:

    • 谢谢侯赛因!
    • 不客气@Tuss,请随时将我的答案标记为已接受。
    猜你喜欢
    • 2023-01-25
    • 2022-10-20
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2014-09-24
    相关资源
    最近更新 更多