Thanks it worked.

I am fairly new to SQLalchemy and this will be my first code. i was
wondering can i map the tables using a mapper to a class and then
somehow use python swap function to swap values ??

On Mar 1, 6:51 pm, Michael Bayer <[email protected]> wrote:
> On Mar 1, 2011, at 9:14 PM, eddy wrote:
>
> > Hi All,
>
> > I have been trying to covert this sql query to sqlalchemy one for
> > hours now with no luck. any help will be appreciated
>
> > "update table1 set columnValue=(case when columnValue=A then B when
> > columnValue=B then A end) where columnValue in (A,B);"
>
> > It is just swapping the  columnValue where its A it set B and where
> > its B it set A
>
> update uses values() for the SET part 
> (docs:http://www.sqlalchemy.org/docs/core/tutorial.html#inserts-and-updates), 
> case is a little weird it takes a list of when/then pairs (docs + 
> exampleshttp://www.sqlalchemy.org/docs/core/expression_api.html?highlight=cas...)
>
> from sqlalchemy.sql import table, column, case
>
> table1 = table('t1', column('columnValue'))
>
> print table1.update().\
>     where(table1.c.columnValue.in_(['A', 'B'])).\
>     values(columnValue=case([
>         (table1.c.columnValue=='A', 'B'),
>         (table1.c.columnValue=='B', 'A')
>     ]
> ))
>
> > I tried
> > query=update([table1.c.columnValue],case([table1.c.columnValue==A, B],
> > [table1.c.columnValue==B, A]), where(table1.c.columnValue==A or
> > table1.c.columnValue==B))
>
> > ???
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "sqlalchemy" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to