1

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?

10
  • Can you please describe, both for the select and the update, the code you are trying to run and the error message you get? Commented May 7, 2015 at 16:27
  • because some error message is chinese,i am afraid you don't understand. Commented May 7, 2015 at 16:57
  • What is userinfo ? 'hello' supposed to do? Do you mean userinfo = 'hello'? In the JSON field? Commented May 7, 2015 at 17:13
  • no,it is postgresql json query grammar,it means all json which has key hello schinckel.net/2014/05/25/querying-json-in-postgres Commented May 7, 2015 at 17:35
  • 3
    This is, by the way, very close to (if not a duplicate of) stackoverflow.com/questions/14779896/… Commented May 7, 2015 at 22:07

1 Answer 1

2

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'"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.