user520987
user520987

Reputation: 41

GAE by using GQL, how to use SQL's like query?

def post(self):

    selector = self.request.get('search')
    search = db.GqlQuery("SELECT * FROM Product WHERE productName = :selector", selector=selector)
    products = search.fetch(10)
    values = {
        'products' : products
        }
    doRender(self, 'search.html', values)

above code is for search function from my Product category... Actually i tried to use the code "Select * From Product Where productName like %:selector%" for my search function, but i couldn't use this code.... Is there anyother GQL code which is substitution of 'SQL-LIKE query'??

Upvotes: 0

Views: 750

Answers (1)

pr4n
pr4n

Reputation: 2968

There is no equivalent of SQL's LIKE in App Engine. You can however do something like

blurred_product_name = selector[:-2]
search = db.GqlQuery('SELECT * FROM Product where productName > ',
                                          blurred_product_name)

So, if you have products with the name Product-1, Product-2, Product-3 and the search term is "Product", your blirred_product_name will be "Produc" which will return all the three possibilities in this case. Be careful with indexes however.

Alternatively you can very well use SearchableModel http://www.billkatz.com/2008/8/A-SearchableModel-for-App-Engine and get this done easily.

Upvotes: 1

Related Questions