Problem with MySQL cursor



Hello,
I have a function that executes a SQL statement with MySQLdb:

def executeSQL(sql, *args):
print sql % args
cursor = conn.cursor()
cursor.execute(sql, args)
cursor.close()

it's called like that:

sql = "INSERT INTO %s (%s) VALUES (%s)"
executeSQL(sql, DOMAIN_TABLE, DOMAIN_FIELD, domainname)

The statement that is printed looks ok (missing quotes, but AFAIK
cursor.execute does that):

INSERT INTO domains (domain) VALUES (xgm.de)

but MySQL prints an error:

Traceback (most recent call last):
File "manage.py", line 90, in ?
addDomain(domainName)
File "manage.py", line 27, in addDomain
executeSQL(sql, DOMAIN_TABLE, DOMAIN_FIELD, domainname)
File "manage.py", line 22, in executeSQL
cursor.execute(sql, args)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in
execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35,
in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 ''domains' ('domain') VALUES ('xgm.de')' at
line 1")

I see the error: 2 opening quotes but only 1 closing around domains. But
where do they come from?

Note that there are no quotes at print sql % args.

Thanks,

Florian
.



Relevant Pages

  • RE: Problem with MySQL cursor
    ... execute, just values ... cursor.execute(sql, args) ... executeSQL(sql, DOMAIN_TABLE, DOMAIN_FIELD, domainname) ... Note that there are no quotes at print sql % args. ...
    (comp.lang.python)
  • Exectuting a binary and getting its output...
    ... I'm creating a program which executes another program sends him some ... input (not through command line args) and then parses its output. ...
    (comp.unix.programmer)
  • Re: Coding Problem (arbitrary thread sych)
    ... peleme wrote: ... So A, B, and C executes the last function ... while D executes it seperate. ... It requires that you know in advance how many threads need to rendezvous so you will have to preprocess your args a little. ...
    (comp.lang.python)