Re: MySQLdb question... using table name as arg
- From: Jim Segrave <jes@xxxxxxxxxxxx>
- Date: Fri, 03 Feb 2006 22:59:46 -0000
In article <%cNEf.19800$sA3.14913@fed1read02>,
Sean Berry <sean@xxxxxxxxxxxxxxxxxx> wrote:
I have four tables that all have the same column names (50 in each.)
I have created an admin program to edit, delete and add records to the
tables and would like to use the table name as a variable in each query so
the code can be used for each of the 4 tables. Usually I would do something
like this by having 1 table with special column to categorize the records as
I am doing with each table, but this specific application requires that I do
it with 4 tables instead.
To ensure that string are quoted properly without any hassle I use the
execute function like so assuming c is my cursor object...
c.execute("update tableName set col1 = %s, col2 = %s, col3 = %s, ...",
(val1, val2, val3, ...))
But, not I want to do this with a variable tableName. If I add it to the
tuple of parameters in the second arg before val1 and replace tableName with
%s, then the tableName will be quoted in the query, causing an error.
What is the best (easiest) way for me to accomplish this? I know it may be
a stupid question but I just can't figure it out.
How about interpolating the table name into the string:
c.execute("update %s set col1 = %%s, col2 = %%s, col3=%%s" % (sometable), \
['the', 'cat', 'in the hat'])
Note the need to double the %'s for the parameters to be bound.
--
Jim Segrave (jes@xxxxxxxxxxxxxx)
.
- References:
- MySQLdb question... using table name as arg
- From: Sean Berry
- MySQLdb question... using table name as arg
- Prev by Date: Re: Automatic class attribute
- Next by Date: Re: web.py + cheetah delivering incomplete pages
- Previous by thread: Re: MySQLdb question... using table name as arg
- Next by thread: Run Windows shortcut
- Index(es):
Relevant Pages
|