我正在尝试使用 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.)))]))
给出异常:断言失败:param count (3) != value count (6)。
但是如果我从表定义和插入行操作中省略时间戳字段,则没有问题。那么我在时间戳方面做错了什么?
最佳答案
(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 是否适合你吗?
关于sqlite - 如何使用 clojure.java.jdbc 插入包含时间戳值的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6798548/