0

我坚持使用 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();
    });
  }
4

1 回答 1

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();
    });
  }
于 2021-11-29T05:31:13.700 回答