> -----Original Message----- > From: sqlalchemy@googlegroups.com > [mailto:sqlalch...@googlegroups.com] On Behalf Of Aref > Sent: 10 June 2010 02:40 > To: sqlalchemy > Subject: [sqlalchemy] help please > > Hello All, > > I just began learning sqlalchemy and am not quite used to it yet so > please excuse my ignorance and which might be a trivial question to > some of you. > I am writing a database module and need to load a table and possibly > modify a record in the table. I can get the connection established and > everything works fine. The problem I am running into is that I do not > necessarily know the column name before hand to code it in the update > method. I want to be able to find out to send a generic column name > which will be updated (gets the column name dynamically). > > I tried the following: > > columns=['ProjectID', 'Program', 'progmanger'] > test = str('table.c.'+columns[1]) > update = table.update(test=='project-name', values = {test:'program'}) > print update > update.execute() > > I get a error when I try to run it. It does not recognize the column > for some reason even though if I print test everything seems to be OK. > I get 'project.c.Program' > > Is there something I am missing here? How can I send the project and > column name to the update method dynamically? > > Thank you so much in advance for any help or insight you > could provide. >
The "table.c" object supports dictionary-style access, so you should be able to use something like this: colname = 'Program' column = table.c[colname] update = table.update(column=='project-name', values = {test:'program'}) However, in general, if you want to get a named attribute of an object, and the name is stored in a variable, you can use Python's "getattr" function. This code should also work: colname = 'Program' column = getattr(table.c, colname) update = table.update(column=='project-name', values = {test:'program'}) Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.