Python SQLAlchemy 和 Postgres - 如何使用 json 值中的子字符串进行搜索

标签 python json postgresql sqlalchemy

我知道我可以使用这个示例查询轻松查询 JSON 值:

r = Books.query.filter(
  Books.nameofjsonfield['key1', 'key2'].astext.cast(Unicode) == 'exact_string_to_compare'
).all()

但我如何搜索与 JSON 值的子字符串匹配的数据集?

最佳答案

将值转换为文本后,很简单using LIKE one way or the other ,或任何其他模糊匹配方法:

r = Books.query.\
    filter(Books.nameofjsonfield['key1', 'key2'].
           astext.
           cast(Unicode).
           contains('not_so_exact_string_to_find')).\
    all()

关于Python SQLAlchemy 和 Postgres - 如何使用 json 值中的子字符串进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055834/

相关文章:

python - 无法使用请求从网页中抓取某个字段的值

python - 将 Python 模块集成到我的代码中的正确方法(Hg,Google Code)

Android JSON解析并存储到数据库

Android,String无法转换为JSONObject

python - 带有嵌套列表的字典 TypeError : string indices must be integers

postgresql - Heroku 数据库还原问题

java - 如何通过 SSL 连接 PostgreSQL 数据库?

postgresql - 在主机上的客户端 docker 和 postgres 服务器之间建立连接

python - Pylint 误报 E1101 : Instance of 'Popen' has no 'poll' member

python - geodjango 与南导致 "duplicate column name error"与 Spatialight 数据库