Python 3 MySQL 语法错误 Select 参数

Python 3 MySQL Syntax Error with Select Parameter

我正在尝试通过 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"

查询通过命令行运行良好,所以我很确定这与我提供字符串参数的方式有关。我做错了什么?

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

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