开发者

How do I escape % from python mysql query

开发者 https://www.devze.com 2023-01-03 14:56 出处:网络
How do I escape the % from a mysql query in python. For example query = \"\"\"SELECT DATE_FORMAT(date_time,\'%Y-%m\') AS dd

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

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.

How开发者_运维问答 do I get mysqldb to ignore the %? Can't see this in any of the docs.


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. %%.

0

精彩评论

暂无评论...
验证码 换一张
取 消