简体   繁体   English

MySQL转义所有内容(所有特殊字符)

[英]MySQL escape everything (all special characters)

I am making a mysql query to select something where a value is LIKE query_search 我正在进行mysql查询,以选择值为LIKE query_search

What I want to achieve is to search the exact query_search passed as a parameter to the query. 我要实现的是搜索作为参数传递给查询的确切query_search For example if query_search is one of the following: %admin , you're\\ , '/_%' , I only want to look for entries with exact matching. 例如,如果query_search是下列之一: %adminyou're\\'/_%' ,我只想查找完全匹配的条目。

For %admin I only want to find the entry %admin , but myadmin should not be found. 对于%admin我只想找到%admin条目,但找不到myadmin

So, how to escape all the mysql special characters? 那么,如何转义所有mysql特殊字符?

Putting \\ in front of each character of query_search would be a good solution? \\放在query_search的每个字符query_search将是一个好的解决方案?

For Django knowers, I am using the 对于Django知识者,我正在使用

something.extra(
   where=['value like convert(_utf8%s using utf8)'],
   params=query_search,
)

where something is a QuerySet. 这里something是一个QuerySet。 I am using MySQL as a DB and I receive query_search as a parameter. 我使用MySQL作为数据库,并且收到query_search作为参数。 Maybe there is another way to achieve this with Django. 也许还有另一种方法可以用Django实现。

Thanks. 谢谢。

您可以在Queryset.filter 使用contains (或icontains ):

something.filter(value__contains=query_search)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM