为什么MySQL连接会忽略R中的SET NAMES utf8指令?

时间:2014-07-07 10:04:56

标签: mysql r utf-8 rmysql

我已从R连接到MySQL数据库并运行“set names utf8”指令。

library(RMySQL)
con<-dbConnect(MySQL(), user='john', password='secret', dbname='awesome', host='myhost')
dbSendQuery(con, "SET NAMES UTF8; ")

当我想从我的查询中返回UTF8数据时,这通常会成功。但是,快速检查表明并非所有变量都已正确设置

dbGetQuery(con, "SHOW VARIABLES LIKE 'character_set_%'")
#             Variable_name                      Value
#1     character_set_client                     latin1
#2 character_set_connection                     latin1
#3   character_set_database                       utf8
#4 character_set_filesystem                     binary
#5    character_set_results                     latin1
#6     character_set_server                     latin1
#7     character_set_system                       utf8

这里显然没有设置character_set_client变量。它也无法明确地设置它。事实上它似乎完全忽略了它。

dbSendQuery(con, "SET character_set_client=utf8; ")
dbGetQuery(con, "SHOW VARIABLES LIKE 'character_set_%'")
#             Variable_name                      Value
#1     character_set_client                     latin1
#2 character_set_connection                     latin1
#3   character_set_database                       utf8
#4 character_set_filesystem                     binary
#5    character_set_results                     latin1
#6     character_set_server                     latin1
#7     character_set_system                       utf8

如何设置此项并使连接返回UTF8编码文本?架构使用utf8编码和整理。

1 个答案:

答案 0 :(得分:1)

发生握手,客户端最终告诉MySQL使用latin1。可以禁用握手,或初始连接SET NAMES utf8

请参阅: http://dev.mysql.com/doc/refman/5.6/en/server-options.html#option_mysqld_character-set-client-handshake