有没有一种很好的方法来执行带有LIKE
in SQLObject 的 SQL 语句?
这个可行,但有点难看:
fields = Foo.select("field LIKE '%%%s%%'" % bar)
其中sqlobject.sqlbuilder
有一个未记录的类LIKE
,可以在其他查询元素中使用。
例如:
from sqlobject.sqlbuilder import LIKE
class Customer(SQLObject):
name = StringCol()
...
# this search is case-dependent
rows = Customer.select(LIKE(Customer.q.name, "%Smith%"))
class ILIKE(LIKE):
op = 'ILIKE'
# this search is case-independent, works on PostgreSQL, not sure about others
rows = Customer.select(ILIKE(Customer.q.name, '%smith%'))
SqlBuilder has a LIKE
function (and also startswith
and endswith
ones that build appropriate LIKE
clauses).