help please

0 views
Skip to first unread message

Aref

unread,
Jun 9, 2010, 9:39:56 PM6/9/10
to sqlalchemy
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.

GHZ

unread,
Jun 10, 2010, 9:21:51 AM6/10/10
to sqlalchemy
you should access column names via lower case

i.e.

columns = 'projectid', 'program', 'progmanger']

Aref

unread,
Jun 10, 2010, 11:29:42 AM6/10/10
to sqlalchemy
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?
> > Thank you so much in advance for any help or insight you could provide.- Hide quoted text -
>
> - Show quoted text -

King Simon-NFHD78

unread,
Jun 10, 2010, 11:36:48 AM6/10/10
to sqlal...@googlegroups.com

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

Lance Edgar

unread,
Jun 10, 2010, 11:37:35 AM6/10/10
to sqlal...@googlegroups.com
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

Aref

unread,
Jun 10, 2010, 12:22:13 PM6/10/10
to sqlalchemy
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.

Lance Edgar

unread,
Jun 10, 2010, 12:38:48 PM6/10/10
to sqlal...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages