【发布时间】:2016-05-06 09:57:15
【问题描述】:
我正在尝试使用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 将多条记录插入到具有 json 列的 postgres 表中的简单方法是什么?
【问题讨论】:
标签: json postgresql jdbc clojure