【问题标题】:How to handle symbols (*, +, ~,...) in sqlalchemy on a flask app如何在烧瓶应用程序的 sqlalchemy 中处理符号(*、+、~、...)
【发布时间】:2019-01-21 15:46:16
【问题描述】:

我有一个完整的网页工作,现在我正在调试错误。直到现在,我唯一无法摆脱的是网页上的输入引入了一个符号(+、*、~、...)

如果我将运算符: op('~* ') 更改为 like,它可以工作,但我不需要 like 运算符,我需要不区分大小写的 op('~* ')

# first, i take from my web the filter that an user have introduced
filter_tag = str(request.form.get('filter_tag'))
if take_filter.filter_tag != '':
    conditions.append(clothes.c.column_tag==filter_tag)

query = session.query(clothes).filter(and_(*conditions),).distinct(clothes.c.nummer).order_by(clothes.c.nummer)

它与普通文本完美配合,但如果用户引入符号(*、~、+、...),则会返回错误:

sqlalchemy.exc.DataError: (psycopg2.DataError) 无效正则表达式:量词操作数无效

【问题讨论】:

  • edit该问题并添加您需要工作的代码,而不是与此相关的其他代码

标签: python python-3.x sqlalchemy flask-sqlalchemy


【解决方案1】:

我根据这篇文章想出了如何做到这一点:Case insensitive with a equal operator ,主要区别是我需要检查一列的值是否包含字符串,而另一篇文章是相等关系(==) .

为此,我将过滤从 op('~*') 更改为包含。但是,包含以区分大小写的方式工作,为了解决这个问题,我只是放入了两个较低的功能。

#import the upper and lower function from sqlalchemy
from sqlalchemy import func

#take the filter from browser
filter_tag = str(request.form.get('filter_tag'))

#Check that the filter is not empty
if take_filter.filter_tag != '':
    conditions.append(func.lower(clothes.c.column_tag).contains(func.lower(filter_tag)))

query = session.query(clothes).filter(and_(*conditions)).distinct(clothes.c.nummer).order_by(clothes.c.nummer)

【讨论】:

    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2021-12-25
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多