pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
947 stars 190 forks source link

How to select with like ??? #824

Closed hongsonitptit closed 4 months ago

hongsonitptit commented 4 months ago

Hello, I tried to add like param to the query as below, but it didn't work.

@Query( 'SELECT Person WHERE name LIKE "%:key%"') --> key = "abc" @Query( 'SELECT Person WHERE name LIKE :key') --> key = "%abc%"

It threw an exception that cannot bind the argument key

However, it still work when I hard code by @Query( 'SELECT * Person WHERE name LIKE "%abc"')

Can you pls check it?

hendrikvdkaaden commented 4 months ago

I tested this myself and your example works fine for me. Is there a chance you might have made a mistake in your code? Here is how I have implemented it:

PersonDao:

@Query('SELECT * FROM persons WHERE name LIKE :key')
Future<List<Person>> findPersonsByName(String key);

main.dart:

persons = widget.personDao.findPersonsByName('%${searchController.text}%');

Could you try this approach and see if it resolves the issue?

hendrikvdkaaden commented 4 months ago
@Query('SELECT * FROM persons WHERE name LIKE '%:key%'')
Future<List<Person>> findPersonsByName(String key);

For this approach in SQLite you can't directly include the % wildcard characters inside the annotation of a query with bound parameters in the manner you've described.

hongsonitptit commented 4 months ago

It works. Thanks a lot