Can't encode chinese properly in console using Rstudio

401 views Asked by At

I am using RODBC to connect mysql with R like below:

library(RODBC)
channel=odbcConnect("MySQL_ODBC_AIRFORECASTSYSTEM",uid="root",pwd = "3896123ray") 
sql="select * from region_station"
ttt=sqlQuery(channel,query = sql)`

Ss you can see I've put the result into a data.frame, "ttt", and I can use View(ttt) to show the contain, and the Chinese shows properly.

enter image description here

However, when I use ttt[,2] trying to show the second column in console, it became like this:

enter image description here

Any help would be greatly appreciated.

The first column is the Chinese, the second column is outcome of mysql's hex(), and the third column is the result in Rstudio's console.

二林站 E4BA8CE69E97E7AB99 鈭\x9e\xab\x99

南投站 E58D97E68A95E7AB99 \xe5\x8d\x8a\xab\x99

埔里站 E59F94E9878CE7AB99 \xe5\x9f\x87\xab\x99

大里站 E5A4A7E9878CE7AB99 憭折\x87\xab\x99

彰化站 E5BDB0E58C96E7AB99 敶啣\x8c\xab\x99

忠明站 E5BFA0E6988EE7AB99 敹\x98\xab\x99

1

There are 1 answers

0
Rick James On BEST ANSWER

950 seems to be Big5. For example

CONVERT(BINARY('大里站') USING big5) --> 憭折 

which agrees with one of your dumps.

So...

SET NAMES big5;

(or however you specify the CHARACTER SET to MySQL from Rstudio)

or change the LC values to be utf8.