我尝试使用 jdbc 从数据库中查询数据。问题是某些列是数组类型。
;get that particular column
(def jdbc-array (with-connection *db*
(with-query-results rs ["select * from refgene limit 5"]
(:exonstarts (first rs)))))
;Check if it has a value
(print jdbc-array)
;#<Jdbc4Array {67075873,67078739,67085754,67100417,67109640,67113051,67129424,67131499,67143471,67162932}>nil
;check class
(class jdbc-array)
;org.postgresql.jdbc4.Jdbc4Array
如何在 clojure 中将此数组转换为 seq/vector?我尝试了 (seq jdbc-array) 和 (seq (.getArray jdbc-array) 但两者都不起作用...
最佳答案
如果 with-connection 选项对您来说很笨重(对我来说就是这样),您可以扩展 IResultSetReadColumn 协议(protocol)以将 Jdbc4Array 对象转换为常规对象或向量:
这是一种方法:
(extend-protocol clojure.java.jdbc/IResultSetReadColumn
org.postgresql.jdbc4.Jdbc4Array
(result-set-read-column [pgobj metadata i]
(vec (.getArray pgobj))))
这将在读取时将所有数组类型转换为向量
这种方法也可以帮助处理 JSON 数据类型,如 this example
关于postgresql - clojure:如何将 jdbc4array 转换为 clojure 的 seq?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6055629/