Thread: [SQLObject] use string to get column value in record
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Sells, F. <fr...@ad...> - 2006-04-26 20:27:50
|
I've got a row from sqlobject using the XXX.get(1) technique. so far so good. now I've got a dictionary of updates that I want to use to update the db, but only if the old data meets certain criteria. So I would like to do something like: row =3D mytable.get(1) for (name,value) in mychanges.items(): =20 if row.col(name) !=3D '&": =20 row.col(name) =3D value I know I can do it with getattr(row, name), but that's ugly (to me). As cool as sqlobject is, there must be a pythonic way of doing this. Any ideas? --------------------------------------------------------------------------- The information contained in this message may be privileged and / or confidential and protected from disclosure. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender immediately by replying to this message and deleting the material from any computer. --------------------------------------------------------------------------- |
From: Oleg B. <ph...@ma...> - 2006-04-26 21:42:14
|
On Wed, Apr 26, 2006 at 04:32:53PM -0400, Sells, Fred wrote: > now I've got a dictionary of updates that I want to use to update the db, > but only if the old data meets certain criteria. So I would like to do > something like: > > row = mytable.get(1) > for (name,value) in mychanges.items(): > if row.col(name) != '&": > row.col(name) = value This will lead to a dozen UPDATEs - one UPDATE for a cycle. Better do row = mytable.get(1) update = {} for (name, value) in mychanges.items(): if getattr(row, name) != '&": # gatattr() is anaviodable here update[name] = value if update: row.set(**update) # UPDATE all values in one query Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |