RMySQL encoding issue on Windows
来源:互联网 发布:游戏里被广为人知的梗 编辑:程序博客网 时间:2024/06/03 19:14
https://stackoverflow.com/questions/43091376/rmysql-encoding-issue-on-windows-spanish-character-%C3%B1
While using RMySQL::dbWriteTable function in R to write a table to MySQL on Windows I get an error message concerning the character [ñ].
The simplified example is:
table <- data.frame(a=seq(1:3), b=c("És", "España", "Compañía")) table a b1 1 És2 2 España3 3 Compañíadb <- dbConnect(MySQL(), user = "####", password = "####", dbname ="test", host= "localhost")RMySQL::dbWriteTable(db, name="test1", table, overwrite=T, append=F )Error in .local(conn, statement, ...) : could not run statement: Invalid utf8 character string: 'Espa'
As you can see, there is no problem with the accents ("És") but there is with the ñ character ("España").
On the other hand, there is no problem with MySQL since this query works fine:
INSERT INTO test.test1 (a,b)values (1, "España");
Things I have already tried previous to write the table:
Encoding(x) <- "UTF-8" for all table.
iconv(x, "UTF-8", "UTF-8") for all table.
Sent pre-query: dbSendQuery(db, "SET NAMES UTF8;")
Change MySQL table Collation to: "utf-8-general, latin-1, latin-1-spanish...)
*Tried "Latin-1" encoding and didn't work either.
I have been looking for an answer to this question for a while with no luck.
Please help!
Versions:
MySQL 5.7.17
R version 3.3.0
Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=C"
PS: Works fine in Linux environment but I am stuck with Windows in my current project :(
1 Answer
This works for me in Windows:
write.csv(table, file = "tmp.csv", fileEncoding = "utf8", quote = FALSE, row.names = FALSE)db <- dbConnect(MySQL(), user = "####", password = "####", dbname ="test", host= "localhost")dbWriteTable( db, value = "tmp.csv", name = "test1", append = TRUE, row.names = FALSE, sep = ",", quote='\"', eol="\r\n")
nvarchar
) but depends on varchar fields with a UTF8 collation. That does lead to problems, if an application saves ASCII data with a different codepage to the field. – Panagiotis Kanavos Mar 29 at 11:01LC_COLLATE
when you have to deal with multiple codepages – Panagiotis Kanavos Mar 29 at 11:08ñ
was converted ton
. R Tools for Visual Studio on the other hand doesn't have this issue, the characters were preserved – Panagiotis Kanavos Mar 29 at 13:20