I want to do a Python MySQL query such as:
cursor = connection.cursor()
sql = "select text \
from steps \
where text like '%start%'"
cursor.execute(sql)
But the % isn't seen as a wildcard, it's expecting a variable, I guess. I get an error:
TypeError: not enough arguments for format string
Nothing seems to work. It would also be nice if I could use a variable instead of '%start%'
I assume you're using python db-api.
You can try escaping %
as %%
.
As for passing the parameters there is a number of ways, for example:
cursor = connection.cursor()
sql = """select text
from steps
where text like %s"""
cursor.execute(sql, (('%' + 'start' + '%',))
You can see examples of the other ways of parameter passing on this blog in "Interface modułu" section. Not all of them work with all connectors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With