选择参数的Python 3 MySQL语法错误

时间:2016-03-30 13:56:56

标签: python mysql mysql-python

我试图通过Flask的MySQLdb模块执行以下操作:

cur.execute("SELECT post_id FROM tbl_post WHERE post_file_path = '%s'", (_filePath,))

然而我收到以下错误:

1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'static/uploads/adc67db4-7d23-4bf1-a7ef-7e34dbed246a.jpg''' at line 1"

通过命令行查询工作正常,所以我相当肯定它与我提供字符串参数的方式有关。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您不应该引用占位符%s,它是由数据库驱动程序完成的。这应该有效:

cur.execute("SELECT post_id FROM tbl_post WHERE post_file_path = %s", (_filePath,))