我正在尝试使用clojure.java.jdbc/insert!
向PostgreSQL数据库中的JSON列插入数据。 我不确定在插入JSON时应使用哪种数据格式。
表定义:
CREATE TABLE errors (
id character varying(24) NOT NULL PRIMARY KEY,
body json NOT NULL
);
尝试在地图格式中使用文字数据:
=> (insert! db :errors {:id "a" :body {:message "A error"}}
{:id "b" :body {:message "B error"}})
PSQLException No hstore extension installed. org.postgresql.jdbc2.AbstractJdbc2Statement.setMap (AbstractJdbc2Statement.java:1709)
另外,作为一个json编码的字符串:
=> (insert! db :errors {:id "a" :body "{\"message\":\"A error\"}"}
{:id "b" :body "{\"message\":\"B error\"}"})
PSQLException ERROR: column "body" is of type json but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 46 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2198)
似乎没有明显的方法来做到这一点。特别重要的是,我需要能够在单个查询中插入多条记录,而不是逐条插入,这是insert!
为我提供的便利。
有什么简单的方法可以使用clojure.java.jdbc将多个记录插入postgres表中的json列中?