Ruby和MySQL UTF-8字符 [英] Ruby and MySQL UTF-8 characters
问题描述
我正在将一个 Sinatra 应用程序从SQLite3切换到MySQL。由于某些原因,我无法理解,当我使用Ruby从MySQL中提取数据时,续集字符以8位ASCII代替UTF-8。
I'm switching a Sinatra application from SQLite3 to MySQL. For some reason, which I can't understand, when I extract data from the MySQL using Ruby and Sequel the characters appear in 8-BIT ASCII instead of UTF-8.
部署环境是FreeBSD 9.1和MySQL 5.6.12,从FreeBSD端口安装了系统范围的ruby19。 RVM ruby-2.0p247产生相同的结果。
The deployment environment is a FreeBSD 9.1 and MySQL 5.6.12, with a system-wide ruby19 installed from FreeBSD ports. RVM ruby-2.0p247 produces the same result though.
我的 my.cnf
如下:
# The following options will be passed to all MySQL clients
[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 32M
thread_cache_size = 4
query_cache_size= 8M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 2
# encoding issues
character-set-server=utf8
collation-server=utf8_general_ci
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
safe-updates
[myisamchk]
key_buffer_size = 64M
sort_buffer_size = 64M
read_buffer = 1M
write_buffer = 1M
[mysqlhotcopy]
interactive-timeout
我的所有文件都使用shebang行以及UTF-8编码就像这个脚本我用来测试条目:
All my files use the shebang line along with the UTF-8 encoding like this script I use to test entries:
#!/usr/bin/env ruby
# encoding: UTF-8
require 'sequel'
msql = Sequel.connect(adapter: 'mysql', host: 'localhost', database: 'metrosignage', user: 'atma', password: 'toola697', encoding: 'utf8')
b = msql[:drama_addressbook]
b.each do |entry|
p entry
# p entry[:city].force_encoding("utf-8")
end
如果我使用条目[:city] .force_encoding(utf-8)
输出正确,希腊UTF-8字符显示罚款但是我不明白为什么我不能直接提取UTF-8。
If I use entry[:city].force_encoding("utf-8")
the output is correct, Greek UTF-8 characters are displayed fine. However I don't understand why I can't extract UTF-8 directly.
我正在读取数据的表是使用以下SQL创建的:
The table I'm reading the data is created using the following SQL:
CREATE TABLE `drama_addressbook` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`address_no` int(11) DEFAULT NULL,
`address_description` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
所以数据库是UTF-8,数据是UTF-8。我的问题是:
So the database is UTF-8 and the data is UTF-8. My questions are:
- 我做错了什么?
- 为什么Ruby需要
force_encoding
?
- Am I doing something wrong?
- Why does Ruby need
force_encoding
?
推荐答案
尝试使用 mysql2
适配器,而不是 mysql
适配器,因为我相信 mysql2
驱动程序处理编码,而 mysql
驱动程序没有。
Try using the mysql2
adapter instead of the mysql
adapter, as I believe the mysql2
driver handles encodings while the mysql
driver does not.
这篇关于Ruby和MySQL UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!