如何使用clojure.java.jdbc插入包含Timestamp值的行?

时间:2011-07-23 05:58:31

标签: sqlite jdbc clojure

我正在尝试使用clojure.java.jdbc将行插入数据库。 (有问题的数据库是sqlite)。

我可以创建一个这样的表:

(def db {:classname "org.sqlite.JDBC"
         :subprotocol "sqlite"
         :subname "/path/to/my/database"})

(with-connection db (create-table :foo [:bar :int] 
                                       [:baz :int] 
                                       [:timestamp :datetime]))

这很有效。但是如果我尝试在数据库中插入一行,则会失败:

(with-connection db (insert-rows :foo
          [1 2 (java.sql.Timestamp. (.getTime (java.util.Date.)))]))

给出异常:断言失败:参数计数(3)!=值计数(6)。

但是如果我省略表定义和插入行操作中的时间戳字段,则没有问题。那么我在时间戳上做错了什么?

1 个答案:

答案 0 :(得分:3)

 (def sqllite-settings
  {
   :classname   "org.sqlite.JDBC"
   :subprotocol "sqlite"
   :subname     "test.db"
  }
 )

(with-connection sqllite-settings 
  (create-table :foo 
        [:bar :int] 
        [:baz :int]  
        [:timestamp :datetime]))

(with-connection sqllite-settings (insert-rows :foo
       [1 2 (java.sql.Timestamp. (.getTime (java.util.Date.)))]))

(with-connection sqllite-settings 
       (with-query-results rs ["select * from foo"] (doall rs)))

返回了预期的:

({:bar 1,:baz 2,:timestamp 1311565709390})

我正在使用clojure.contrib.sql

(use 'clojure.contrib.sql)

来自此处的SQLLite驱动程序:http://www.zentus.com/sqlitejdbc/

你能尝试使用contrib.sql吗?

相关问题