简体   繁体   English

如何在 mysql 连接器 python 查询中获得文字“%”?

[英]How do I get a literal '%' in a mysql connector python query?

Consider this code:考虑这个代码:

def search(searchstr: str, limit: int=10, offset: int=0):
    csr = app.sql.cursor(dictionary=True)
    csr.execute("select username, fname, bio from followers where username like %%%s%% limit %d offset %d", (searchstr, limit, offset))
    return csr.fetchall()

search("j")

where app.sql is a reference to the connection object.其中app.sql是对连接对象的引用。

I would like it to execute something similiar to: select username, fname, bio from followers where username like %j% limit 10 offset 0 but instead it always gives me the error:我希望它执行类似于以下内容的操作: select username, fname, bio from followers where username like %j% limit 10 offset 0但它总是给我错误:

  File "/---/user.py", line 67, in search
    csr.execute("select username, fname, avatarUrl, bio from followers where username like %%%s%% limit %d offset %d", (searchstr, limit, offset))
  File "/---/venv/lib/python3.8/site-packages/mysql/connector/cursor.py", line 542, in execute
    raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

I can't figure out what I am doing wrong because我无法弄清楚我做错了什么,因为

>>> "%%%s%% %d %d" % ("j", 10, 0)
"%j% 10 0"

which is the expected output.这是预期的输出。 How can I fix this?我怎样才能解决这个问题?

Placeholders do not work like copy and paste, you can't just place them anywhere and expect them to be understood.占位符不像复制和粘贴那样工作,你不能把它们放在任何地方并期望它们被理解。 Even if it did work like that, in your case you'd end up with like %foo% , which is invalid SQL syntax.即使它确实像那样工作,在你的情况下你最终会like %foo% ,这是无效的 SQL 语法。

You need to put one string placeholder into the query, and provide a string as argument which contains the LIKE wildcards:您需要将一个字符串占位符放入查询中,并提供一个字符串作为包含LIKE通配符的参数:

csr.execute('... LIKE %s ...', (f'%{searchstr}%', ...))

In other words, you supply the % wildcards from Python, not from within the SQL statement.换句话说,您从 Python 中提供%通配符,而不是从 SQL 语句中提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何成功安装 mysql 和 mysql-python 连接器? - How do I successfully install mysql and mysql-python connector? 如何将 mysql-connector-python 与 MAMP 一起使用? - How do I use mysql-connector-python with MAMP? 如何在 mysql python 连接器中放置列名称的占位符? - How do I put a placeholder for a column name in mysql python connector? Python 3:如何获得字节字符串的字符串文字表示? - Python 3: How do I get a string literal representation of a byte string? 如何通过 mysql.connector 将 python 与 mysql 数据库连接时解决错误? - How do I solve error whlie connecting python with mysql database via mysql.connector? 如何使用 MySQL-Connector 从 Python 中的 MySQL 存储过程中检索输出参数? - How do I retrieve an out parameter from a MySQL Stored Procedure in Python using MySQL-Connector? python MySQL连接器和参数化查询 - python MySQL connector and parameterized query 尝试使用mysql python连接器执行预准备语句时,我得到NotImplementedError - I get NotImplementedError when trying to do a prepared statement with mysql python connector 如何在python中将str文字转换为int文字? - How do I change a str literal into a int literal in python? mysql / connector python查询在mysql中有效,但在python中不起作用 - mysql/connector python query works in mysql but not python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM