MySQL query fails in Python
The following query works on MySQL Query Analyzer and PHP but fails in Python:
update home_recipient
set
process = 'PROCESS_ID', processed_on = current_timestamp
where
sent_on is null or
timestampdiff(minute, processed_on, current_timestamp) > 60
order by id limit 45
The error displayed is "You can't specify target table 'home_recipient' for update in FROM clause"
I'm using Python 2.7 and MySQL_python-1.2.3-py2.7.egg
The error seems to be generated by Python's driver because que query works everywhere else.
Is there a way to tell MySQL_python to stop interpreting the query and pass it "as is" to MySQL?
EDIT: This is the code that executes the query.
sql = """update home_recipient set process = 'PROCESS_ID', processed_on = current_timestamp
where sent_on is null or timestampdiff(minute, processed_on, current_timestamp) > 60
order by id limit 45"""
cursor = connection.cursor()
cursor.execute(sql)
OK. This is getting ridiculous. Even removing everything else from the update shows the same error:
sql = """update home_recipient set process = 'PROCESS_ID', processed_on = current_timestamp开发者_StackOverflow"""
cursor = connection.cursor()
cursor.execute(sql)
Any help?
Thanks
The three " are the problem
""" This is a string for documentation"""
http://docs.python.org/tutorial/introduction.html
curs.execute("update home_recipient set process = 'PROCESS_ID', processed_on = current_timestamp where sent_on is null or timestampdiff(minute, processed_on, current_timestamp) > 60 order by id limit 45")
If PROCESS_ID is a variable:
curs.execute( 'update home_recipient set process=:PROCESS_ID, processed_on = current_timestamp where sent_on is null or timestampdiff(minute, processed_on, current_timestamp) > 60 order by id limit 45', [PROCESS_ID])
精彩评论