0

Is there a pretty way to execute an SQL statement with a LIKE in SQLObject?

This one works, but it's somewhat ugly:

    fields = Foo.select("field LIKE '%%%s%%'" % bar)
Alex
  • 43,191
  • 44
  • 96
  • 127

2 Answers2

1

In sqlobject.sqlbuilder there is an undocumented class called LIKE which can be used amongst other query elements.

For example:

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%'))
jonafato
  • 1,595
  • 14
  • 21
0

SqlBuilder has a LIKE function (and also startswith and endswith ones that build appropriate LIKE clauses).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395