Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong character encoding in R console

I have a problem with encoding after fetching data from MySQL database. Please see attached code:

mydb = dbConnect(MySQL(), user='root', password='', dbname='', host='localhost')
dbGetQuery(mydb, "SET NAMES 'utf8'")
rs = dbSendQuery(mydb, "SELECT body_prepared FROM articles")
data = fetch(rs, n=-1)

print(data)    
internetovĂ˝ televĂ­zor mĂşdry obyÄŤajnĂ˝

After used command print(data) I should see internetový televízor múdry obyčajný(It is slovak languague), but I see internetovĂ˝ televĂ­zor mĂşdry obyÄŤajnĂ.

I tried to run R via Rgui and also via Rstudio, but that didn't have any effect and result was same in Rgui and also Rstudio. I tried to use enc2utf8(data[[1]]) or change various types encoding by Sys.setlocale("LC_CTYPE", "en_EN.UTF-8"), but these commands werent helpful. I thought, there is problem on Mysql side, but there is set correct utf-8 encoding for my column.

I don't know where can be the problem. It's made me mad. Can you help with this problem? I will be very gratuful.

For complete information, here is my sessionInfo:

> sessionInfo()
    R version 3.1.2 (2014-10-31)
    Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Slovak_Slovakia.1250  LC_CTYPE=Slovak_Slovakia.1250   
[3] LC_MONETARY=Slovak_Slovakia.1250 LC_NUMERIC=C                    
[5] LC_TIME=Slovak_Slovakia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RMySQL_0.10.2 DBI_0.3.1    

loaded via a namespace (and not attached):
[1] tools_3.1.2

UPDATE

I rewrote dbGetQuery(mydb, "SET NAMES 'utf8'") to dbGetQuery(mydb, "SET NAMES 'cp1250'") and suddenly it showed correct result.

like image 830
peteruherek Avatar asked Jul 28 '26 17:07

peteruherek


1 Answers

The data was stored incorrectly. Probably you had the text encoded in utf8 bytes, but SET NAMES was latin1 or cp1250 when you did the INSERT, and the CHARACTER SET of the column is latin1 or cp1250. Is all of this correct? If so, we can proceed with fixing the data. If not, please provide details.

Also, provide SELECT col, HEX(col) ... to verify what bytes were stored. And SHOW CREATE TABLE to see the details of the column.

like image 78
Rick James Avatar answered Jul 30 '26 10:07

Rick James