Clojure 에서 postgreSQL 사용하기 (java.jdbc)

버들비·2021년 4월 20일

project.clj 에 디펜던시 추가하기

:dependencies [[org.clojure/clojure "1.10.0"]
                 [org.clojure/java.jdbc "0.6.1"]
                 [org.postgresql/postgresql "9.4-1201-jdbc41"]]

postgresql을 실행하고, testdb 라는 db를 생성한다.

clojure로 SQL 명령하기

먼저 jdbc 의 네임스페이스를 sql로 지정.
[clojure.java.jdbc :as sql]

테이블 만들기

(sql/db-do-commands "postgresql://localhost:5432/testdb"
                    (sql/create-table-ddl :testing [[:id :int] [:data :text]]))

id와 data 라는 항목을 가진 testing 테이블이 만들어진다.

INSERT 하기

(sql/insert! "postgresql://localhost:5432/testdb"
             :testing {:id 1 :data "Hello second!"})

SELECT 하기

(sql/query "postgresql://localhost:5432/testdb"
           ["select data from testing"])

0개의 댓글