2

次の例を参照してください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 ...");
      }
    }
4

2 に答える 2

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))))
于 2013-12-08T19:35:11.983 に答える