Clojure/JDBC/Postgresql:我试图从字符串更新 postgresql 中的时间戳值,出现错误
将 clojure jdbc 库与 postgresql 结合使用。我在 postgresql 中有一个带有时间戳列“created_at”的表“xxx”,并且有一个包含正确格式的日期的字符串。执行插入失败:
(require '[clojure.java.jdbc :as sql])
(sql/with-connection *db*
(sql/insert-record :xxx {:created_at "Thu Feb 09 10:38:01 +0000 2012"}))
这是错误:
org.postgresql.util.PSQLException: ERROR: column "created_at"
is of type timestamp with time zone but expression is of type character varying
所以我知道 postgres 需要时间戳值,但是如何将日期的字符串表示形式转换为 postgres 会接受的内容? java.util.Date 也失败了,我在 clojure postgres 库上找不到任何文档。
谢谢!
Using the clojure jdbc library with postgresql. I have a table "xxx" with a timestamp column "created_at" in postgresql, and I have a string containing a date in the right format. Doing an insert fails:
(require '[clojure.java.jdbc :as sql])
(sql/with-connection *db*
(sql/insert-record :xxx {:created_at "Thu Feb 09 10:38:01 +0000 2012"}))
Here is the error:
org.postgresql.util.PSQLException: ERROR: column "created_at"
is of type timestamp with time zone but expression is of type character varying
So I understand that postgres requires a timestamp value, but how do I convert my string representation of the date into something postgres will accept? java.util.Date fails also, and I can't find any docs on the clojure postgres library.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要传入一个 java.sql.Timestamp 实例。要使用
clj-time
将字符串解析为一个,Joda-Time-Clojure 的包装库,您可以按照以下方式执行操作:然后返回的值可以通过 JDBC 传递到 PostgreSQL。
如果您以其他字符串格式获取日期并将其转换为这种格式,则可以跳过转换并为原始表示形式提供适当的格式化程序。默认情况下,
clj-time.format/formatters
映射中提供了相当多的可用选项,例如 REPL 中的(clj-time.format/show-formatters)
即可查看包含示例的列表。此外,clj-time.coerce/from-string
按顺序尝试所有默认格式化程序,返回第一个后续解析的值(nil
如果没有)。如果您要获取java.util.Date
或long
形式的日期,请参阅from-date
和from-long
在同一个命名空间中。如果您决定使用
clj-time
,请使用[clj-time "0.3.6"]
作为project.clj
中的依赖项说明符。或者,您可以使用其他方式将时间戳字符串解析为
java.sql.Timestamp
;Timestamp
本身可以解析不同的字符串表示形式:clj-time
是 Clojure 中处理日期和时间的最明智的方式,因此它可能值得您花时间。You'll need to pass in a
java.sql.Timestamp
instance. To parse your string into one usingclj-time
, a Joda-Time-wrapping library for Clojure, you'd do something along the following lines:The returned value can then be passed to PostgreSQL via JDBC.
In case you're obtaining the date in some other string format and converting it to this one, you could skip the conversion and provide an appropriate formatter for the original representation. There are quite a few available by default in the
clj-time.format/formatters
map, say(clj-time.format/show-formatters)
at the REPL to see a list with examples. Also,clj-time.coerce/from-string
tries all default formatters in sequence returning the value of the first succeeding parse (nil
if there is none). If you're obtaining the date as ajava.util.Date
or along
, seefrom-date
andfrom-long
in the same namespace.Use
[clj-time "0.3.6"]
as the dependency specifier in yourproject.clj
if you decide to useclj-time
.Alternatively, you could use some other way of parsing your timestamp string into a
java.sql.Timestamp
;Timestamp
itself can parse a different string representation:clj-time
is the most sane way of dealing with date and time in Clojure, though, so it's likely to be worth your while.