On 8 Sep 2013, at 02:36, Mohsen Pahlevanzadeh <m.pahlevanza...@gmail.com> wrote:

> Suppose i need to send a set of table field and a value to search, i found 
> the following code:
> ///////////////////////////////////////
> q = session.query(myClass)
> for attr, value in web_dict.items():
> 
>     q 
> = q.filter(getattr(myClass, attr).like("%%%s%%" % value))
> ///////////////////////////////////
> 
> 
> But it have problem,
> 
> i'm newbie in python can you investigate it?

I'm not sure if you understand how the above code is supposed to work, so I'll 
explain it. It assumes that "web_dict" is a dictionary mapping names of 
attributes of myClass to strings, and it will return instances of myClass where 
all the named attributes contain those values as substrings.

So, for example, if myClass had "name" and "phonenumber" properties, and 
assuming that web_dict looks like this:

web_dict = {
    'name': 'Mohsen',
    'phonenumber': '11111',
}

web_dict.items() returns a list like this:

  [('name', 'Mohsen'), ('phonenumber', '11111')]

So the first time round the "for" loop, "attr" will be set to "name" and 
"value" will be "Mohsen". The second time round, "attr" will be "phonenumber" 
and "value" will be "11111".

(Note that dictionaries aren't ordered, so web_dict.items() could actually 
return the pairs in a different order)

Let's break the loop down into a few more lines:

for attr, value in web_dict.items():
    column = getattr(myClass, attr)
    condition = column.like('%%%s%%' % value)
    q = q.filter(condition)

"getattr" is a python function that retrieves a named attribute from an object. 
So:

    getattr(myClass, "phonenumber")

is exactly equivalent to:

    myClass.phonenumber

So the first time round the loop, "getattr(myClass, attr)" returns 
myClass.name, and the second time round it will return myClass.phonenumber.

(http://docs.python.org/2/library/functions.html#getattr)


"'%%%s%%' % value" is a python string-formatting function. "%s" will be 
replaced by whatever is in "value", and "%%" becomes "%". So in our example 
above, the first time round the loop we will have the string "%Mohsen%' and the 
second time round it will be "%11111%".

(http://docs.python.org/2/library/stdtypes.html#string-formatting)

That string is passed to the <column>.like function, which produces SQL such as 
'name LIKE "%Mohsen%"'. That condition is then passed to Query.filter, which 
will add it to the WHERE clause of the SQL using AND.

So the code will eventually end up doing something like this:

q = session.query(myClass)
q = q.filter(myClass.name.like('%Mohsen%'))
q = q.filter(myClass.phonenumber.like('%11111%'))

which will end up producing SQL something like:

SELECT <myClass columns>
FROM <myClass table>
WHERE name LIKE '%Mohsen%'
AND phonenumber LIKE '%11111%'

Is that the sort of SQL that you are hoping to see, or do you need something 
different?

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to