简体   繁体   中英

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.

For example, I have a table user, and a field 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

SELECT * FROM user WHERE data ? 'hello';

not by jdbc/execute direct write sql like

(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 ?

If you have the latest postgresql driver you can escape the ? with a double ??

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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