[sqlalchemy] Re: help please
you should access column names via lower case i.e. columns = 'projectid', 'program', 'progmanger'] On 10 Jun, 03:39, Aref arefnamm...@gmail.com wrote: 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. -- 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.
[sqlalchemy] Re: help please
Thank you for the response. However, that is not the problem. If I do update = table.update(project.c.ProjectID=='project-name', values = {project.c.ProjectID:'program'}) print update update.execute() everything works fine. if I do this: test = 'table.c.'+columns[0] #columns is a list which contains the columns names update = table.update(test == 'project-name', values={test:'program'}) update.execute() it does not work. I get an error that there is no such column. I need to be able to update columns dynamically where I do not have a prior knowledge of what tables and what are the table columns that may exist. How can I do that if at all? On Jun 10, 7:21 am, GHZ geraint.willi...@gmail.com wrote: you should access column names via lower case i.e. columns = 'projectid', 'program', 'progmanger'] On 10 Jun, 03:39, Aref arefnamm...@gmail.com wrote: 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.- Hide quoted text - - Show quoted text - -- 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.
Re: [sqlalchemy] Re: help please
On 6/10/2010 10:29 AM, Aref wrote: Thank you for the response. However, that is not the problem. If I do update = table.update(project.c.ProjectID=='project-name', values = {project.c.ProjectID:'program'}) print update update.execute() everything works fine. if I do this: test = 'table.c.'+columns[0] #columns is a list which contains the columns names update = table.update(test == 'project-name', values={test:'program'}) update.execute() it does not work. I get an error that there is no such column. I need to be able to update columns dynamically where I do not have a prior knowledge of what tables and what are the table columns that may exist. How can I do that if at all? Instead try: update = table.update(eval(test)=='project-name', values={test:'program'}) I can't say for sure that's the best way to do it still, but it would solve your immediate problem. The "test" variable is referencing a string, not a column. You have to eval() it to get the column reference. However, you say your error is that "there is no such column" ... I'd expect a much different error if my suggestion were to actually fix your problem. Anyway good luck. :) Might include your traceback next time if you still have problems. Lance -- 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.
[sqlalchemy] Re: help please
Thank you very much. I'll try it. Is there a better way of doing this-- I mean there must be since this is necessary for any application needing to modify a database where generally tables are accessed dynamically. On Jun 10, 9:37 am, Lance Edgar lance.ed...@gmail.com wrote: On 6/10/2010 10:29 AM, Aref wrote:Thank you for the response. However, that is not the problem. If I do update = table.update(project.c.ProjectID=='project-name', values = {project.c.ProjectID:'program'}) print update update.execute() everything works fine. if I do this: test = 'table.c.'+columns[0] #columns is a list which contains the columns names update = table.update(test == 'project-name', values={test:'program'}) update.execute() it does not work. I get an error that there is no such column. I need to be able to update columns dynamically where I do not have a prior knowledge of what tables and what are the table columns that may exist. How can I do that if at all? Instead try:update = table.update(eval(test)=='project-name', values={test:'program'})I can't say for sure that's the best way to do it still, but it would solve your immediate problem. The test variable is referencing a string, not a column. You have to eval() it to get the column reference. However, you say your error is that there is no such column ... I'd expect a much different error if my suggestion were to actually fix your problem. Anyway good luck. :) Might include your traceback next time if you still have problems. Lance -- 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.
Re: [sqlalchemy] Re: help please
On 6/10/2010 11:22 AM, Aref wrote: Thank you very much. I'll try it. Is there a better way of doing this-- I mean there must be since this is necessary for any application needing to modify a database where generally tables are accessed dynamically. Well, I suspect the "better way" would be to follow King Simon's advice (http://groups.google.com/group/sqlalchemy/msg/b8c0f6bce263ff3d?hl=en) and avoid the eval() stuff as well. Instead you'd be using getattr() or accessing the table.c collection dictionary-style. Lance -- 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.