Skip to main content
 首页 » 编程设计

python之如何在 peewee 查询的文本字段中搜索子字符串

2024年10月01日25lhb25

我正在尝试使用 peewee 返回在 Sqlite 数据库中特定项目的文本字段中具有特定短语或单词的结果。

我目前的尝试是:

for key in listOfKeys: 
    foundPun = models.Pun.select().where(key in str(models.Pun.keywords)).get() 
    click.echo('-'*10) 
    click.echo(foundPun.pun) 
    click.echo('-'*10) 

返回错误: AttributeError: 'bool' 对象没有属性 'clone'

作为引用,这是 Pun 模型:

class Pun(Model): 
    pun = TextField() 
    keywords = TextField(default="") 
    tags = TextField(default="") 
 
class Meta: 
    database = db 

这是否是在 peewee 中搜索结果的正确方法?

非常感谢任何帮助或为我指明正确的方向!

请您参考如下方法:

编辑: 使用列出的查询运算符 here ,特别是 .contains(substr)

原创:

使用fn.substr,fn文档为here .

一个类似的问题和更彻底的答案是 here .