Ruby MySQL gem UTF-8
Monday, June 28th, 2010I just spent a ton of time figuring this out, so I figured I’d toss it up on the blog in case someone else needs it.
Problem: I was exporting and manipulating the data in a MySQL database using the ruby mysql gem. Unfortunately, the people writing the data in the database were using curly quotes and other typographic marks that were causing trouble.
Solution:
require ‘rubygems’
require ‘mysql’
@dbh = Mysql.init
@dbh.options(Mysql::SET_CHARSET_NAME, ‘utf8′)
@dbh.real_connect(”localhost”,@dbuser,@dbpass,@dbname)
The key is to add a line in the options call to set the character set to UTF-8. This seems to solve the problem.
Hope this helps!

