在带有python peewee和postgresq的整数列上使用contains

2024-10-01 15:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的postgresql数据库中有一个表,其中有一个数据类型为integer的列。我想使用peeweepythonorm执行.contains操作。在

它似乎不起作用,因为在进行比较之前需要将整数值转换为字符串。在postgresql中,您可以执行value::text,然后整数值将是一个字符串,您可以在like表达式中使用它。在

我可以使用peeweeorm来完成这个任务,先转换值,然后使用.contains吗?或者有别的办法?在

例如,我想匹配2022022年的20个

谢谢


Tags: 字符串text数据库value表达式postgresql整数integer
3条回答

我看到了一些答案,但我将添加我自己的.02作为库的开发人员。在

from playhouse.shortcuts import cast

query.where(cast(Url.http_status_code, 'text').contains(search_val))

我不确定哪一个更有效(可能是_-CHAR),但是如果您正在寻找更通用的东西,您可以使用cast。在

def python_partial_match(needle,key,haystack):
     for item in haystack.select():
         if needle in str(getattr(item,key,None)): 
            yield item

for result in python_partial_match("20","phone_number",UserModelClass):
    print "GOT RESULT:",result

但我认为对于大数据集来说,这将是非常缓慢的

或者我觉得你可以

^{pr2}$

也许。。。我没有测试演员的角色。。。(见http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=cast#Node.extend

实际上,它看起来好像有一个内置的cast在游戏室里为博士后看http://docs.peewee-orm.com/en/latest/peewee/playhouse.html?highlight=cast#cast

我找到了另一种方法。我还在上面测试了coleifer的解决方案,效果很好。 最后我使用了postgresql格式化函数https://www.postgresql.org/docs/9.3/static/functions-formatting.html

query.where(fn.TO_CHAR(Url.http_status_code, '999').contains(search_val))

相关问题 更多 >

    热门问题