Drop Table w/ MySQLdb?

MRAB python at mrabarnett.plus.com
Sun Jun 6 11:40:37 EDT 2010


Victor Subervi wrote:
> Hi;
> I tried this:
> 
>     cursor.execute('drop table tmp%s', tmpTable)
> 
> and got this error:
> 
> Traceback (most recent call last):
>   File "/var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py>", line 196, in ?
>     cart()
>   File "/var/www/html/angrynates.com/cart/cart.py 
> <http://angrynates.com/cart/cart.py>", line 189, in cart
>     cursor.execute('drop table tmp%s', tmpTable)
>   File "/usr/lib64/python2.4/site-packages/MySQLdb/cursors.py", line 
> 163, in execute
>     self.errorhandler(self, exc, value)
>   File "/usr/lib64/python2.4/site-packages/MySQLdb/connections.py", line 
> 35, in defaulterrorhandler
>     raise errorclass, errorvalue
> ProgrammingError: (1064, "You have an error in your SQL syntax; check 
> the manual that corresponds to your MySQL server version for the right 
> syntax to use near ''127541158007'' at line 1")
> 
> But when I print that statement out (exchanging the comma for a %) and 
> manually enter it:
> 
> mysql> drop table tmp127541158007;
> Query OK, 0 rows affected (0.00 sec)
> 
> I am able to successfully drop the table. Why?
> 
As has been explained already, SQL might not (and here it clearly does
not) let you use placeholders for table or column names, only for
values.

Build the SQL statement with placeholders for the values (any values
which aren't constants) and then execute the SQL statement, passing the
values so that .execute performs the substitution itself.



More information about the Python-list mailing list