Kris
Kris

Reputation: 19948

How to create a database using JDBC in Clojure?

This is what I have tried, starting with clojure.java.jdbc and a map containing the database connection details:

(:require '[clojure.java.jdbc :as j]))

(def mysql-db {:dbtype "mysql", :dbname "changelog_development", :user "root", :password "", :useSSL true, :verifyServerCertificate false}

First I tried to use execute but it cannot use the given connection configuration because the database does not yet exist:

(j/execute! mysql-db "CREATE DATABASE changelog_development") ;; MySQLSyntaxErrorException Unknown database 'changelog_development'    

So I remove that dbname key and tried again, however the error says I have a missing parameter:

(j/execute! (dissoc mysql-db :dbname) "CREATE DATABASE changelog_development") ;; IllegalArgumentException db-spec {:dbtype "mysql", :user "root", :password "", :useSSL true, :verifyServerCertificate false} is missing a required parameter  

Upvotes: 2

Views: 477

Answers (1)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

clojure.java.jdbc connection spec has many possible formats. The one with keys for :dbtype, :user etc requires that you also specify a :dbname.

You can also specify your connection spec using a connection URI instead and execute your statement again:

{:connection-uri "jdbc:mysql://localhost/?user=root&password="}

Upvotes: 4

Related Questions