MySQL查询在Python中失败

时间:2011-09-09 23:18:00

标签: python mysql

以下查询适用于MySQL查询分析器和PHP但在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

显示的错误是“您无法在FROM子句中为更新指定目标表'home_recipient'”

我正在使用Python 2.7和MySQL_python-1.2.3-py2.7.egg

错误似乎是由Python的驱动程序生成的,因为查询在其他任何地方都有效。

有没有办法告诉MySQL_python停止解释查询并将其“按原样”传递给MySQL?

编辑:这是执行查询的代码。

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)

行。这太荒谬了。即使从更新中删除其他所有内容也会显示相同的错误:

sql = """update home_recipient set process = 'PROCESS_ID', processed_on = current_timestamp"""
cursor = connection.cursor()
cursor.execute(sql)

任何帮助?

由于

1 个答案:

答案 0 :(得分:0)

这三个“是问题

""" 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")

如果PROCESS_ID是变量:

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])