简体   繁体   English

如何使用clojure.java.jdbc从Postgresql查询JSON数据类型?

[英]How do I query a JSON datatype from postgresql with clojure.java.jdbc?

I understand how to: 我了解如何:

But I don't know how to put it together and query json with clojure.java.jdbc. 但我不知道如何将其放在一起并使用clojure.java.jdbc查询json。

For example, I have a table user, and a field user_info 例如,我有一个表用户和一个字段user_info

CREATE TABLE user (id SERIAL,user_info JSON);

then I found this blog to impl some protocol,and it insert success! 然后我发现这个博客暗示了一些协议,它成功插入了!

(insert! conn :yxt_user {:user_info {:hello [1 "test" true]}})

But I don't know how to write code to query it like this sql from jdbc/query 但是我不知道如何编写代码来从jdbc / query像这样的sql查询它

SELECT * FROM user WHERE data ? 'hello';

not by jdbc/execute direct write sql like 不是通过jdbc / execute直接写sql之类的

(jdbc/execute! db-spec ["UPDATE table SET col1 = NOW() WHERE id = ?" 77])

I tried to write this query 我试图写这个查询

(jdbc/query conn ["SELECT * FROM user WHERE user_info ? 'hello'"])

I got this 我懂了

org.postgresql.util.PSQLException: 未设定参数值 1 的内容。

Then I tried 然后我尝试

(jdbc/query conn ["SELECT * FROM user WHERE user_info ? 'hello'" "?"])

I got this 我懂了

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"

How do I write a query to filter on a JSON column where user_info has the JSON key hello ? 如何编写查询以对user_info具有JSON密钥hello的JSON列进行过滤?

If you have the latest postgresql driver you can escape the ? 如果您有最新的postgresql驱动程序,则可以转义? with a double ?? 与双??

This should work: 这应该工作:

 (jdbc/query conn ["SELECT * FROM user WHERE user_info ?? 'hello'"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM