how to make Query, which is always "True" (1=1 or so) ?

36 views
Skip to first unread message

Jurgis Pralgauskis

unread,
Nov 24, 2016, 1:03:10 AM11/24/16
to web2py-users
Hi,

   I dynamically generate queries from my search form fields/filters.
   and then map reduce the query list...
   But if no filter is selected, I get empty list...

  So I  try to add dummy query to the beginning:
  Query(db, db._adapter.EQ, 1, 1)

  but I get
AttributeError: 'SQLite' object has no attribute 'EQ'
Message has been deleted

Marlysson Silva

unread,
Nov 24, 2016, 6:15:06 AM11/24/16
to web2py-users
How are you making your query map reduce?

Jurgis Pralgauskis

unread,
Nov 25, 2016, 7:32:46 AM11/25/16
to web2py-users

How are you making your query map reduce?


query = reduce(lambda a, b: (a & b), queries)

Martin Weissenboeck

unread,
Nov 25, 2016, 10:14:09 AM11/25/16
to web2py-users
Could this solve the problem?

db.define_table("mytable", Field("myvalue"))

mytable contains:

3 selected


----------------------------------------------------------------
def testqueries():
    queries=[True]
    qa = db.mytable.myvalue.contains("a")
    qb = db.mytable.myvalue.contains("b")
    qc = db.mytable.myvalue.contains("c")
    queries.append(qa)
    queries.append(qb)
    w = db(reduce(lambda a,b: (a&b), queries)).select(db.mytable.ALL)
    return locals()

Testqueries

qa:
<Query (LOWER(mytable.myvalue) LIKE '%a%' ESCAPE '\')>
qb:
<Query (LOWER(mytable.myvalue) LIKE '%b%' ESCAPE '\')>
qc:
<Query (LOWER(mytable.myvalue) LIKE '%c%' ESCAPE '\')>
queries:
True
<Query (LOWER(mytable.myvalue) LIKE '%a%' ESCAPE '\')>
<Query (LOWER(mytable.myvalue) LIKE '%b%' ESCAPE '\')>
w:
mytable.idmytable.myvalue
1abc

----------------------------------------------------------------
def testqueries():
    queries=[True]
    w = db(reduce(lambda a,b: (a&b), queries)).select(db.mytable.ALL)
    return locals()

Testqueries

queries:
True
w:
mytable.idmytable.myvalue
1abc
2bcd
3def


----------------------------------------------------------------

Regards, Martin


2016-11-25 13:32 GMT+01:00 Jurgis Pralgauskis <jurgis.pr...@gmail.com>:

How are you making your query map reduce?


query = reduce(lambda a, b: (a & b), queries)

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.




Anthony

unread,
Nov 25, 2016, 10:46:44 AM11/25/16
to web2py-users
db.mytable.id > 0

Anthony

Anthony

unread,
Nov 25, 2016, 10:52:18 AM11/25/16
to web2py-users
You can also simply specify a query as raw SQL:

db(('1 = 1')).select(db.mytable.ALL)

Anthony
Reply all
Reply to author
Forward
0 new messages