我使用 Python 2.7 并mysql.connector
在 pyCharm 中运行
我需要使用此处和此处所示的参数化查询
鉴于这些示例,下面似乎cursor.execute("SELECT * FROM automatedReports WHERE pythonFunctionName = %s", (function_name))
应该可以工作。
但是,当我在 pyCharm 中编写此行时,我收到此错误:
检查说:
如果我运行代码,我会收到此错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '%s' 附近使用正确的语法
这是完整的代码:
class DatabaseManager():
def get_report_settings_for_function_named(self, function_name):
"""
Get the settings for a given report from the database based on the name of the function that was called
to process the report
This is how we get the, email subject and email addresses to send the report to
:param function_name: The name of the function that was called to process the report
:type function_name: str.
:returns: array -- the settings row from the database.
"""
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor(dictionary=True)
cursor.execute("SELECT * FROM automatedReports WHERE pythonFunctionName = %s", (function_name))
row = cursor.fetchone()
cursor.close()
cnx.close()
print cursor.statement
return row
print DatabaseManager().get_report_settings_for_function_named('process_elation_credit_report')
我在这里做错了什么?这是旧语法吗?我不会这么认为...
请注意,如果我将值输入到查询字符串中,它一切正常,只是不会让我使用参数。