Jump to navigation

Nikola Plejić

Forcing Korma to use UTF-8 in MySQL connections

2015-02-04 in clojure

Fixing non-ASCII inserts.

I'm a huge fan of Korma, a Clojure DSL for relational databases. It makes handling connections (c3p0 for the win!) and writing queries in Clojure a breeze, and so far I haven't found any huge drawbacks.

One of the issues I have run into is inserting UTF-8 encoded data — any non-ASCII characters would end up as a question mark ("?") in the database. This led me to believe that Korma isn't defaulting to the UTF-8 encoding, so I went my merry way to find a way to convince it to do so. GitHub Issue #150 pointed me in the right direction, but the ultimate solution was lurking in the somewhat unrelated Issue #213.

I've used the default (defdb quux (mysql ...)) way of establishing a connection, but that one doesn't allow us to define the encoding. The trick is to pass the entire "raw" connection map by hand:

(defdb quux {:classname "com.mysql.jdbc.Driver"
             :subprotocol "mysql"
             :subname "//localhost:3306/quux"
             :useUnicode "yes"
             :characterEncoding "UTF-8"
             :delimiters "`"
             :user "zerocool"
             :password "hunter2"})

That's about it! Happy hacking.