如何使用python的sqlalchemy的execute()和pymysql在查询中转义%? [英] How to escape % in a query using python's sqlalchemy's execute() and pymysql?
本文介绍了如何使用python的sqlalchemy的execute()和pymysql在查询中转义%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的查询是:
result = connection.execute(
"select id_number from Table where string like '_stringStart%' limit 1;")
给出错误:
query = query % escaped_args
TypeError: not enough arguments for format string
一个快速的Google表示使用%%而不是%,但这也不起作用.我该如何转义%或有另一种方法来查询以随机字母开头然后是特定顺序的字符串?
A quick google said to use %% instead of % but that doesn't work either. How do I escape the % or is there another way to query for a string that starts with a random letter then a certain sequence?
推荐答案
Since this is a literal string, you're better off using a bound parameter here (illustrated using text()
):
from sqlalchemy import text
connection.execute(
text("select * from table where "
"string like :string limit 1"),
string="_stringStart%")
这篇关于如何使用python的sqlalchemy的execute()和pymysql在查询中转义%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文