我想做一个Python MySQL查询,例如:
cursor = connection.cursor()
sql = "select text \
from steps \
where text like '%start%'"
cursor.execute(sql)
Run Code Online (Sandbox Code Playgroud)
但我认为%不被视为通配符,它期待变量.我收到一个错误:
TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)
似乎没什么用.如果我可以使用变量而不是'%start%'那也很好
sou*_*eck 20
我假设您正在使用python db-api.
您可以尝试转义%为%%.
至于传递参数,有很多种方法,例如:
cursor = connection.cursor()
sql = """select text
from steps
where text like %s"""
cursor.execute(sql, (('%' + 'start' + '%',))
Run Code Online (Sandbox Code Playgroud)
您可以在"Interfacemodułu"部分中查看此博客上其他参数传递方式的示例.并非所有连接器都适用.