我在Clojure.java.jdbc
(sql/db-do-prepared db "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )" ["test" "test" 1 1.0])
但是如何将java
代码转换为clojure
代码。我是clojure
的新手,不知道如何传递多个vector
final int numRows = 10000;
PreparedStatement pstmt = conn
.prepareStatement("insert into new_order values (?, ?, ?)");
for (int id = 1; id <= numRows; id++) {
pstmt.setInt(1, id % 98);
pstmt.setInt(2, id % 98);
pstmt.setInt(3, id);
int count;
if ((count = pstmt.executeUpdate()) != 1) {
System.err.println("unexpected update count for a single insert " + count);
System.exit(2);
}
if ((id % 500) == 0) {
System.out.println("Completed " + id + " inserts ...");
}
}
答案 0 :(得分:5)
对于多个向量,函数是varargs:
(sql/db-do-prepared db "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )" ["test" "test" 1 1.0] ["test" "test" 2 3.0])
如果要从列表生成输入,可以使用apply:
(apply sql/db-do-prepared db "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )" (for [i (range 10)] ["test" "test" i 1.0]))
对于该逻辑的文字复制,您不能使用varargs,因为它不会让您有机会在下一次操作之前检查每个返回值:
(let [num-rows 1000
success
(reduce
(fn [state id]
(let [values [(mod id 98)
(mod id 98)
id]
[result] (sql/do-prepared "insert into test values (?)" values)]
(if (not= result 1)
{:ok id}
(reduced {:error result}))))
(range 1 (inc num-rows)))]
(if-let [id (:ok success)]
(println "Completed" id "inserts")
(do (println "unexpected update count for a single insert" (:error success))
(System/exit 2))))
答案 1 :(得分:0)
就我而言,我认为@noisesmith是正确的,除了我认为sql-params
应该在集合(向量)中。
(SQL / DB-DO-制备 D b [“INSERT INTO fruit2(名称,外观,成本,等级)VALUES(?,?,?,?)” “测试”“测试”1 1.0])
我认为这对我有用(我错过了什么请告诉我)
或者自2013年以来API可能已更改