What methods to use a database from Clojure are there?
I know from Clojure you can do anything you can with Java, but that means that I may end up using something overly
Nowadays there are Datomic and many similar solutions like DataScript for very interesting relational (non-sql!) database capabilities as well.
The latest and greatest for SQL databases seems to be HoneySQL and Yesql.
HoneySQL is a quite elegant DSL to generate SQL queries. There are rumours it can even modify the statements to be highly optimized, see the clojure-group thread "Current best-of-breed JDBC libraries?" from Feb 24 2015.
Niels van Klaveren says in the above-mentioned thread:
"Basically, it [HoneySQL] generates SQL scripts to relink foreign key references to clean up duplicates in a database. It takes a honeysql select query with (at least) a from
table, a group-by
and an order-by
clause as a base definition what are to be considered doubles, and in which order records should be preserved. In combination with JDBC metadata that query effectively gets rewritten to generate:
To create the best performing, but still database independent SQL, I had to extend honeysql with extra clauses like OVER
and PARTITION BY
. I wouldn't say it was a breeze, but seemed to work very well.
...
That cut down SQL to (sometimes) GB's of script to around a few 100 lines of SQL, and on one occasion, a runtime from 19 hours to 1.5 minutes."
Yesql, on the other hand, aim for total simplicity. It defines some functions to load parameterized .sql
-files.
It's webpage mentions the following "USPs":
(raw-sql "some('funky'::SYNTAX)")
function.If you are open to using a Java library but want something that embraces simplicity, perhaps you'll like Persist. It'll only take you 10 minutes to have a look and see if it fits your needs.
If you need persistent connections and/or connections to multiple databases and do not want to reestablish connections every so often I would recommend using DB connection pools. Something like BoneCP or Tomcat CP.
You can supply DataSources from those packages to (clojure.contrib.sql/with-connection ...).
Firstly, Import libraries from
(ns clojureexercise.test
(:require [clojure.java.jdbc :as sql])) ;;sql will alias used further in code to access java jdbc feature.
Secondly, the function below allows you to connect MySQL server. Like in Java, we declare Database variable to start DB, here it is the same way we have to define Database connectivity and in below code you can see db variable is defined in the dbconnect function. db variable will be used further in running queries.
(defn dbconnect []
(def db{
:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//127.0.0.1:3306/testdb" ;;testdb is the name of database
:user "root"
:password "password"}))
Now,
;;Inserting Data into Database
;;Table Name is patientinfo, consist columns {id, firstname, lastname, birthdate, gender}
(defn insertdata []
(sql/insert! db :patientinfo ;;used sql alias and db variable to insert data into table
{:id 1 :firstname "Satyam" :lastname "Ramawat"
:birthdate "1/1/2018" :gender "Male" }))
I will elaborate this more:
sql/insert! db :patientinfo
sql will enable insert query functionality and db will allow the system to understand on which table the record has to be inserted of which database connection.
I'd like to add an as-of-Nov-2011 answer for the sake of anybody coming here from Google.
The current core SQL access library in Clojure 1.3 is clojure.java.jdbc. There are some very good libraries built on top of this like ClojureQL and Korma.