Reputation: 453
I'm trying to run a delete query using MySQLdb and passing 3 variables. The query is run as follows:
self.cursor.execute("""DELETE er.* FROM extension_recording er,
extension e, client c
WHERE er.extension_id = e.id AND e.client_id = c.id
AND c.parent_client_id = %s
AND DATE(er.cr_date) BETWEEN '%s' AND '%s'""" ,
(client_id, start_date, end_date))
Please excuse the indenting, couldn't seem to make it legible without.
And what happens is this: TypeError: not all arguments converted during string formatting
I've tried passing the SQL to the cursor as a string (I know this is vulnerable to SQL injection, was just testing) and the result is the same. I've examined the SQL and it seems fine.
Is this something to do with the single quoted dates in the query? Or something else?
Upvotes: 0
Views: 671
Reputation: 41950
There's a clue in the traceback...
Traceback (most recent call last):
File "/usr/local/bin/recording_archive_newer.py", line 194, in <module>
print "Error %s deleting from DB" % (sys.exc_info())
TypeError: not all arguments converted during string formatting
The problem is not with the query, but with printing the error message.
sys.exc_info()
returns a tuple of three elements, but you've only specified one placeholder in the string "Error %s deleting from DB"
.
It's worth noting that (sys.exc_info())
is not a tuple with one element, but is interpreted as sys.exc_info()
. If you want to make it a one-element tuple, you need a trailing comma, i.e. (sys.exc_info(),)
.
However, if that line is part of a block like...
try:
# do query
except:
print "Error %s deleting from DB" % (sys.exc_info())
...you'd be better off re-raising the original exception, otherwise it'll be really difficult to work out where the actual problem is. I'd suggest changing it to something like...
try:
# do query
except:
print "Error deleting from DB"
raise
...at least while you're debugging.
Upvotes: 1