Ruby MySQL gem UTF-8

June 28th, 2010

I 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!

Fix things that seem to be broken

April 15th, 2010

Boost Rails performance by generating constants

March 11th, 2010

BPA

February 8th, 2010

Whatcha doin’ this weekend? Sharding the cabinet.

January 29th, 2010

Make everything as simple as possible but no simpler

January 26th, 2010

Serendeputy state of the union

November 19th, 2009

Still Alive!

November 9th, 2009

Linky Goodness - 8/27/2009

August 27th, 2009

Linky Goodness - 8/17/2009

August 17th, 2009

Linky Goodness - 8/14/2009

August 14th, 2009

I hope Yahoo BOSS doesn’t go away

July 30th, 2009

Missionary CEOs

July 28th, 2009

Linky Goodness - 7/27/2009

July 27th, 2009

Serendeputy public beta launch

July 15th, 2009

Site’s back up

July 3rd, 2009

Site down

July 2nd, 2009

Workaround for IE7 mouseout bug

June 30th, 2009

Lots of smart people thinking the same way

June 25th, 2009

Built out the vocabulary engine

June 24th, 2009

Three cheers for crushing self-doubt

June 16th, 2009

Getting closer

June 16th, 2009

Tables in Emacs Org Mode

May 29th, 2009

My current favorite topics list

May 26th, 2009

My current favorite sources list

May 26th, 2009

Watch out for American Express?

May 4th, 2009

Linky Goodness - 4/29/2009

April 29th, 2009

How to prevent browsers from caching a page in Rails

April 14th, 2009

Where am I?

April 9th, 2009

Getting through the Dip

February 23rd, 2009

Fan Mail: SEO Toolbar

January 28th, 2009

Moving closer to alpha

January 27th, 2009

Reinventing classifieds

January 14th, 2009

Serendeputy on Twitter

January 5th, 2009

Boston.com’s Newton Site

December 12th, 2008

Not a good sign

November 19th, 2008

Times Tags

October 22nd, 2008

Digg coming to my turf

October 20th, 2008

Why you need to own your email

October 20th, 2008

The sublime joy of bug-fixing

September 26th, 2008

How to reload a class in irb

September 16th, 2008

Stack Overflow is pretty impressive

September 16th, 2008

Progress update - 9/15/08

September 15th, 2008

How to test tag attributes using assert_select

September 10th, 2008

How to get Subversion to ignore your Rails log files

September 5th, 2008

Revving the prototypes

August 25th, 2008

Good user experience for potentially-objectionable pictures

August 11th, 2008

Linky Goodness - 7/7/2008

July 7th, 2008

Linky Goodness - 7/1/2008

July 1st, 2008

Fan Mail: Twit Podcasts

June 27th, 2008