django postgres LIKE查询语法错误在“%”或附近 [英] django postgres LIKE query syntax error at or near "%"

查看:101
本文介绍了django postgres LIKE查询语法错误在“%”或附近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,我要查询JSONB:

In django, to query JSONB, I do:

    cursor.execute("""\
    SELECT * FROM "somemodel_some_model" 
    WHERE UPPER(("somemodel_some_model"."data" 
    #>> array[0,'fieldX'])::text[]) 
    LIKE UPPER(% %s %)
    """,[my_string])

。我得到:

IndexError: list index out of range

我知道上述内容不能成立,因为当我使用ORM通过以下方式实现此目标时:

I know the above cant be true because when I use the ORM to achive this using:

obj=Some_Model.objects.filter(data__0__fieldX__icontains=search_term)

。得到结果。
要对此进行调查,我使用SQL并执行以下操作:

.. I get the results. To investigate this, I drop to SQL and do the following:

SELECT * FROM "somemodel_some_model"
WHERE UPPER(("somemodel_some_model"."data" 
#>> array[0, 'fieldX'])::text[]) 
LIKE UPPER(%my_search_string%)

..但是,我得到了:

..but, I get:

django。 db.utils.ProgrammingError:%或附近的语法错误

django.db.utils.ProgrammingError: syntax error at or near "%"

所以,问题是,我是否需要转义%?似乎很奇怪

So, the question is, do I need to escape %? Seems odd

推荐答案

在使用cursor.execute之类的语法时,应使用 %% string %% 或如果您使用列和LIKE %% ||列|| %%

When using cursor.execute like syntax should be LIKE %% string %% or if you use column and LIKE %% || column || %%

这篇关于django postgres LIKE查询语法错误在“%”或附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆