在 sqlalchemy ilike 语句中使用变量
Use variable in sqlalchemy ilike statement
我正在尝试查询我的 database
以查找与我的变量 received_input
.
匹配的所有项目
目前我有:
session.query(VenueItem).filter(VenueItem.venue_item_name.ilike("%received_input%")).all()
我的 database
中的项目可能包含 uppercase
和 lowercase
个字符。我需要确保搜索是 case-insensitive
("ApPle"
将 return 来自 "apple"
的 input
)。
我不知道在 ilike
中指定 variable
而不是 string
的语法。
将"%received_input%"
替换为
'%{}%'.format(received_input)
在 python 3.6+ 上,可以使用 f-strings:
更简洁地编写
示例:
f'%{received_input}%'
我正在尝试查询我的 database
以查找与我的变量 received_input
.
目前我有:
session.query(VenueItem).filter(VenueItem.venue_item_name.ilike("%received_input%")).all()
我的 database
中的项目可能包含 uppercase
和 lowercase
个字符。我需要确保搜索是 case-insensitive
("ApPle"
将 return 来自 "apple"
的 input
)。
我不知道在 ilike
中指定 variable
而不是 string
的语法。
将"%received_input%"
替换为
'%{}%'.format(received_input)
在 python 3.6+ 上,可以使用 f-strings:
更简洁地编写示例:
f'%{received_input}%'