Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to patch load SQL statements from a file using clojure.java.jdbc?

I need to be able to load and execute a file with multiple SQL statements in clojure. For example, lets say I have a file with statements like:

ALTER TABLE bla...;
ALTER TABLE foo...;
UPDATE bla SET ...;
UPDATE foo SET ...;
ALTER TABLE bla DROP...;
ALTER TABLE foo DROP...;

You get the idea-- a file with many statements that are semicolon terminated.

I'm currently getting the following error when trying to use do-commands:

PSQLException org.postgresql.util.PSQLException: Too many update results were returned.
like image 665
Damon Snyder Avatar asked Jan 30 '26 19:01

Damon Snyder


1 Answers

The way I ended up solving this was like so:

(ns myns.db
  (:require [clojure.java.jdbc :as sql]            
            [clojure.java.io :refer [resource]]))

(defn db-conn [] ...)

(defn exec-sql-file  
   [file]  
   (sql/with-connection (db-conn)
    (sql/do-prepared
      (slurp (resource file)))))

...

; from your lein project where src/sql/some-statements.sql is the file you want to load
(exec-sql-file "sql/some-statements.sql")

I would be interested to hear how others have handled this problem. Is there a better way?

like image 67
Damon Snyder Avatar answered Feb 01 '26 14:02

Damon Snyder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!