from Massimo, in another reply today (https://groups.google.com/forum/#!topic/web2py/RqSBO2dZU4E) :
query &= db.flatpage.f==request.function is the same as query = query & (db.flatpage.f==request.function) Build a query from another query On Saturday, March 28, 2015 at 11:14:44 AM UTC-5, gb wrote: > > What is the DAL Code to generate a query like this: > SELECT * FROM product WHERE product.id=1 OR product.id=2 OR product.id=3 > OR product.id product.id=4 OR product.id=5; > or even: > SELECT * FROM product WHERE (product.id=1) OR (product.id=2) OR ( > product.id=3) OR (product.id=4) OR (product.id=5); > > I've tried both the DAL code approaches below and I always end up with > crazy parentheses nesting which will eventually crash the DB driver e.g. in > sqlite: <class 'sqlite3.OperationalError'> parser stack overflow > > Is there another way to use the DAL, or is my only option to write the > SQL manually? > > db.define_table("product") > > selected_ids = [1,2,3,4,5] > query = [] > for pid in selected_ids: > query.append(db.product.id == pid) > > query = reduce(lambda a,b:a|b,query) > #Ouputs this : (((((product.id = 1) OR (product.id = 2)) OR ( > product.id = 3)) OR (product.id = 4)) OR (product.id = 5)) > > > > selected_ids = [1,2,3,4,5] > query = [] > for pid in selected_ids: > query |= db.product.id == pid > > #I get a bonus parenthesis with this method ((((((product.id = 1) OR > ) OR (product.id = 2)) OR (product.id = 3)) OR (product.id = 4)) OR ( > product.id = 5)) > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.