我正在将Sinatra应用程序从SQLite3切换到MySQL.由于某些我无法理解的原因,当我使用Ruby和Sequel从MySQL提取数据时,字符以8-BIT ASCII而不是UTF-8出现.部署环境是FreeBSD 9.1和MySQL 5.6.12,从FreeBSD端口安装了系统...
我正在将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驱动程序则不会.
本文标题为:Ruby和MySQL UTF-8字符
- R语言绘图数据可视化pie chart饼图 2022-12-10
- Ruby on Rails在Ping ++ 平台实现支付 2023-07-22
- Ruby 迭代器知识汇总 2023-07-23
- Golang http.Client设置超时 2023-09-05
- Go Web开发进阶实战(gin框架) 2023-09-06
- 汇编语言程序设计之根据输入改变屏幕颜色的代码 2023-07-06
- Ruby的字符串与数组求最大值的相关问题讨论 2023-07-22
- R语言-如何切换科学计数法和更换小数点位数 2022-11-23
- Swift超详细讲解指针 2023-07-08
- R语言关于二项分布知识点总结 2022-11-30