r mysql utf8,从R中的MySQL中获取UTF-8文本返回“????”

I'm stuck trying to fetch UTF-8 text in a MySQL database from R. I'm running R on OS X (tried both via the GUI and command line), where the default locale is en_US.UTF-8, and no matter what I try, the query result shows "?" for all non-ASCII characters.

I've tried setting options(encoding='UTF-8'), DBMSencoding='UTF-8' when connecting via ODBC, setting Encoding(res$str)

I'm totally stumped. Any ideas why it's not working, or other things I should try?

Here's a fairly minimal test case:

$ mysql -u root

mysql> CREATE DATABASE test;

mysql> USE test;

mysql> CREATE TABLE test (str VARCHAR(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO test (str) VALUES ('こんにちは');

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+-----------------+

| str |

+-----------------+

| こんにちは |

+-----------------+

1 row in set (0.00 sec)

Querying the table in R using both RODBC and RMySQL shows "?????" for the str column:

> con

> sqlQuery(con, 'SELECT * FROM rtest.test')

str

1 ?????

> library(RMySQL)

Loading required package: DBI

> con

> dbGetQuery(con, 'SELECT * FROM rtest.test')

str

1 ?????

For completeness, here's my sessionInfo:

> sessionInfo()

R version 2.15.1 (2012-06-22)

Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:

[1] stats graphics grDevices utils datasets methods base

other attached packages:

[1] RMySQL_0.9-3 DBI_0.2-5 RODBC_1.3-6

解决方案

Thanks to @chooban I found out the connection session was using latin1 instead of utf8. Here are two solutions I found:

For RMySQL, after connecting run the query SET NAMES utf8 to change the connection character set.

For RODBC, connect using CharSet=utf8 in the DSN string. I was not able to run SET NAMES via ODBC.

This question pointed me in the right direction.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值