如何从python mysql查询中转义%

时间:2021-05-30 00:07:57

How do I escape the % from a mysql query in python.

如何从python中的mysql查询中转义%。

For example

例如

query = """SELECT DATE_FORMAT(date_time,'%Y-%m') AS dd
FROM some_table
WHERE some_col = %s
AND other_col = %s;"""

cur.execute(query, (pram1, pram2))

gives me a "ValueError: unsupported format character 'Y'" exception.

给我一个“ValueError:不支持的格式字符'Y'”异常。

How do I get mysqldb to ignore the %? Can't see this in any of the docs.

如何让mysqldb忽略%?在任何文档中都看不到这一点。

1 个解决方案

#1


25  

Literal escaping is recommended by the docs:

文档建议使用文字转义:

Note that any literal percent signs in the query string passed to execute() must be escaped, i.e. %%.

请注意,传递给execute()的查询字符串中的任何文字百分号都必须进行转义,即%%。

#1


25  

Literal escaping is recommended by the docs:

文档建议使用文字转义:

Note that any literal percent signs in the query string passed to execute() must be escaped, i.e. %%.

请注意,传递给execute()的查询字符串中的任何文字百分号都必须进行转义,即%%。