1

I'm trying to get data from mysql with mysql/mysqlplus ruby gems. Here's the code:

$ cat 1.rb
#!/home/yuri/.rvm/wrappers/ruby-1.9.3-p385@test/ruby
require 'mysql'
db = Mysql.init
db.options Mysql::SET_CHARSET_NAME, 'utf8'
db = Mysql.real_connect 'localhost', 'root', 'password'
db.select_db 'site'
db.query "SET NAMES utf8"
res = db.query 'SELECT * FROM users LIMIT 1'
p res.fetch_hash['non_latin_field'].encoding
p db.character_set_name

$ ./1.rb
#<Encoding:ASCII-8BIT>
"latin1"

The same goes for mysqlplus. Expected result is as follows:

$ ./1.rb
#<Encoding:UTF-8>
"utf8"

Am I doing it wrong?

UPD

To clarify matters:

$ mysql --help | grep ^default-character-set
default-character-set             utf8
$ mysql -u root -p -BNe "SHOW VARIABLES LIKE 'character_set\_%'"
Enter password: 
character_set_client    utf8
character_set_connection        utf8
character_set_database  utf8
character_set_filesystem        binary
character_set_results   utf8
character_set_server    utf8
character_set_system    utf8

$ mysql -u root -p -e "CREATE DATABASE d1 CHARACTER SET = utf8"
Enter password:
$ mysql -u root -p -e "CREATE TABLE t1 (f1 VARCHAR(255) CHARACTER SET utf8)" d1
Enter password:
$ mysql -u root -p -e "INSERT INTO t1 (f1) VALUE ('уключина')" d1
Enter password:

$ cat 1.php
#!/usr/bin/env php
<?php
$args = array_slice($_SERVER['argv'], 1);
mysql_connect('localhost', 'root', '...');
mysql_select_db('d1');
$r = mysql_query('SELECT f1 FROM t1');
echo mysql_fetch_assoc($r)['f1'];

$ ./1.php
уключина

$ cat 1.rb
#!/home/yuri/.rvm/wrappers/ruby-1.9.3-p385@test/ruby
require 'mysql'
db = Mysql.new 'localhost', 'root', 'fdfnfh'
if ARGV.include? '--explicit'
    db.query "SET NAMES utf8"
end
db.select_db 'd1'
f1 = db.query('SELECT * FROM t1 LIMIT 1').fetch_hash['f1']
if ARGV.include? '--force-encoding'
    f1.force_encoding 'utf-8'
end
p f1
db.query("SHOW VARIABLES LIKE 'character_set\\_%'").each do |row|
    p row
end

$ ./1.rb
"????????"
["character_set_client", "latin1"]
["character_set_connection", "latin1"]
["character_set_database", "utf8"]
["character_set_filesystem", "binary"]
["character_set_results", "latin1"]
["character_set_server", "utf8"]
["character_set_system", "utf8"]

$ ./1.rb --explicit
"\xD1\x83\xD0\xBA\xD0\xBB\xD1\x8E\xD1\x87\xD0\xB8\xD0\xBD\xD0\xB0"
["character_set_client", "utf8"]
["character_set_connection", "utf8"]
["character_set_database", "utf8"]
["character_set_filesystem", "binary"]
["character_set_results", "utf8"]
["character_set_server", "utf8"]
["character_set_system", "utf8"]

$ ./1.rb --explicit --force-encoding
"уключина"
["character_set_client", "utf8"]
["character_set_connection", "utf8"]
["character_set_database", "utf8"]
["character_set_filesystem", "binary"]
["character_set_results", "utf8"]
["character_set_server", "utf8"]
["character_set_system", "utf8"]

So the problem apparently lies not in mysql, but in how to make these gems use encoding other than ASCII-8BIT.

x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • Can you make sure your 1.rb script has # encoding: UTF-8 right under #!/home/yuri/.rvm/wrappers/ruby-1.9.3-p385@test/ruby just in case. – rainkinz Nov 08 '13 at 16:53
  • `# encoding: UTF-8` specifies [script encoding](http://www.ruby-doc.org/core-2.0.0/Encoding.html#label-Script+encoding), it has nothing to do with the output. Either way, I've confirmed that it doesn't helps. – x-yuri Nov 09 '13 at 00:58

1 Answers1

0

Disregard my previous answer. I see that the author of the mysql gem updated it: https://github.com/tmtm/ruby-mysql

gem install ruby-mysql

gem uninstall mysql

ruby 1.rb
#<Encoding:UTF-8>
"utf8"
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • "`mysql2` is meant as a more modern replacement for the existing `mysql` gem, which has been stale for a while now. I also heard that the author isn't supporting it anymore and instead recommends everyone use his pure-ruby version since it's compatible with more Ruby implementations (but is much slower)." [Ruby, Rails: mysql2 gem, does somebody use this gem? Is it stable?](http://stackoverflow.com/a/3003679/52499). So I'd rather go with `mysql2`. – x-yuri Nov 09 '13 at 01:22
  • I had a case where mysql gem returned US-ASCII ruby Strings whereas mysql2 returned UTF-8 as it should be. – Pascal Mar 19 '14 at 16:16