Thank you for your suggestions.

An acceptable solution (for me):

def sql_query_with_and(query1, query2):
    if (None == query2):
        return query1
    else:
        return query1 & query2


query = None

if (None != field1_value):
    query = (db.table.field1 == field1_value)
if (None != field2_value):
    query = sql_query_with_and((db.table.field2 == field2_value),
query)
if (None != field3_value):
    query = sql_query_with_and((db.table.field3 == field3_value),
query)

rows = db(query).select()


I'm a happy web2py user!



On 18 Iul, 22:00, Yarko Tymciurak <yark...@gmail.com> wrote:
> since form.vars holds validated request.vars,
>
> after form.accepts( ),
>
> look at the structure of your form.vars, and then you'll see what you need
> to do to build up your query from the user response.
>
> you can show them in your page, for development, with
> {{=BEAUTIFY(form.vars)}} and compare that with {{=BEAUTIFY(request.vars)}}
>
> On Sat, Jul 18, 2009 at 8:29 PM, ionel <ionelanton...@gmail.com> wrote:
>
> > I think I wasn't clear. The user can make any selection to search the
> > database.
>
> > Search Form example:
>
> > [*] field1 = some_value1
> > [ ] field2
> > [*] field3 = some_value2
> > [ ] field4
> > [ ] field5
> > [ ] field6
> > etc.
>
> > So, for this situation:
> > query = (db.table.field1=='some_value1')&
> > (db.table.field3=='same_value2')
>
> > Next time he can change the selection:
>
> > [ ] field1
> > [*] field2 = some_value1
> > [ ] field3
> > [ ] field4
> > [*] field5 = some_value2
> > [*] field6 = some_value3
>
> > query = (db.table.field2=='some_value1')&
> > (db.table.field5=='same_value2')&(db.table.field5=='same_value3')
>
> > I do not know what the user will select in advance. I need to generate
> > the query after his selection, in any field combination.
>
> > On Jul 18, 9:14 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> > > yes
>
> > > db(db.table.field0=='somevalue').select
> > > (db.table.field1,db.table.field2,orderby=db.table.field3)
>
> > > On Jul 18, 7:58 pm, ionel <ionelanton...@gmail.com> wrote:
>
> > > > Just to be more clear. The user can select multiples fields, from one
> > > > to all.
>
> > > > On Jul 18, 8:55 pm, ionel <ionelanton...@gmail.com> wrote:
>
> > > > > Hello everyone,
>
> > > > > I have a table with multiples fields (20). The user can select the
> > > > > fields for searching the table. He can select one field or all the
> > > > > fields in any combination. I need to create a query with his
> > > > > selection. Is this thing possible in web2py but without using
> > > > > executesql() ?
>
> > > > > Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to