Using utf8 encoding in Ruby with MySQL 5.1 and 5.5

remember to require mysql gem first
require "rubygems"
require 'mysql'

with MySQL 5.1
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME,"utf8")
db.real_connect("localhost", "username", "password", "database")

with MySQL 5.5
db = Mysql.real_connect("localhost", "username", "password", "database")
db.charset = "utf8"

check the result:
puts db.character_set_name

If you got utf8, Congratulations!

留言

這個網誌中的熱門文章

神之雫 的 雫 (雨下) 怎麼唸、意思是什麼

[已解決] 入肉 (肏) 的注音/怎麼唸?意思是什麼?(兒童不宜)

哆啦A夢放上任何東西都無違和感、彷彿有音效的滑鼠墊 這裡買得到