views:

536

answers:

3

When using Grails 1.1 together with a MySQL the charsets of the auto-generated database tables seem to default to ISO-8859-1. I'd rather have everything stored as pure UTF-8. Is that possible?

From the auto-generated database definitions:

ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

Note the "latin1" part.

A work-around that works for Grails 1.0 is described here. This work-arounds does not appear to work when using Grails 1.1. The dataSource.dialect parameter seems to be ignored.

+2  A: 

It seems like the default character set of the database is used.

I solved this by creating the database with utf8 as the default character set:

CREATE DATABASE name_of_database DEFAULT CHARACTER SET utf8;
knorv
A: 

You can also add some properties to the Hibernate / JDBC url in your DataSource.groovy file. For example

url = "jdbc:mysql://localhost:3306/your-db-name?useUnicode=true"

MySQL has other parameters related to character sets as well. I'm not quite sure which and how many of them you need to get the expected behavior.

Kimble
If the database's default character set is not utf8, this won't work.
Ferdinand Chan
+1  A: 

Hi I was having similar problem where reading from MySQL was fine but writing converted some characters to "?" (question mark). This solved it: url = "jdbc:mysql://localhost:3306/your-db-name?useUnicode=true&characterEncoding=utf-8

mh23