Bra*_*och 12 postgresql json jdbc clojure
我正在尝试使用在postgresql数据库中插入json列clojure.java.jdbc/insert!.我不确定在插入json时应该使用哪种数据格式.
表定义:
CREATE TABLE errors (
id character varying(24) NOT NULL PRIMARY KEY,
body json NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
尝试以地图格式使用文字数据:
=> (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)
Run Code Online (Sandbox Code Playgroud)
或者作为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)
Run Code Online (Sandbox Code Playgroud)
似乎没有明显的方法来做到这一点.特别重要的是,我需要能够在一个查询中插入许多记录而不是逐个插入,insert!为我提供了便利.
使用clojure.java.jdbc将多个记录插入到具有json列的postgres表中的简单方法是什么?
由于clojure.java.jdbc公开了一些协议,包括clojure.java.jdbc/ISQLValue,你可以扩展它以允许提供json作为普通的clojure映射.Travis Vachon提供了对此过程的详尽说明以及实现该过程的代码段.
我还找到了一个库clj-postgresql,它实现了许多额外的postgres功能,包括json和jsonb数据类型.您可以简单地要求clj-postgresql.types并导入json支持.
| 归档时间: |
|
| 查看次数: |
1955 次 |
| 最近记录: |