我正在将Sinatra应用程序从sqlite3切换到MysqL.由于某些我无法理解的原因,当我使用Ruby和Sequel从MysqL提取数据时,字符以8-BIT ASCII而不是UTF-8出现.
部署环境是FreeBSD 9.1和MysqL 5.6.12,从FreeBSD端口安装了系统范围的ruby19. RVM ruby-2.0p247产生相同的结果.
我的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编码,例如我用来测试条目的以下脚本:
#!/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
如果我使用entry [:city] .force_encoding(“ utf-8”)输出是正确的,希腊UTF-8字符将显示正常.但是我不明白为什么我不能直接提取UTF-8.
我正在读取数据的表是使用以下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.我的问题是:
>我做错什么了吗?
> Ruby为什么需要force_encoding?
解决方法:
尝试使用MysqL2适配器而不是MysqL适配器,因为我相信MysqL2驱动程序会处理编码,而MysqL驱动程序则不会.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。