我在MYSQL data.base上有一个带有名字和姓氏的表,其中UTF-8(?)编码在加载到R时无法正确呈现 有关信息,数据库编码如下所示
我尝试使用RODBC在Rstudio中用R查询它:
第一次尝试:
library(RODBC)
library(data.table)
conn <- odbcConnect("my_db", DBMSencoding = "")
sqlStr <- "SELECT id, first_name, surname from my_table;"
result <- setDT(sqlQuery(conn, sqlStr, stringsAsFactors = FALSE))
x <- result[id == 12]
x
# id first_name surname
# 1: 12 Roberto Batti\xcc_n
Encoding(x$surname)
# "unknown"
Encoding(x[["surname"]] <- "latin1"
x
# id first_name surname
# 1: 12 Roberto BattiÌ_n
预期结果是:
# id first_name surname
# 1: 12 Roberto BATTIÓN
第二次尝试:尝试更改连接中的DBMS编码
conn <- odbcConnect("my_db", DBMSencoding = "latin1")
sqlStr <- "SELECT id, first_name, surname from my_table;"
result <- setDT(sqlQuery(conn, sqlStr, stringsAsFactors = FALSE))
x <- result[id == 12]
x
# id first_name surname
# 1: 12 Roberto BattiÌ_n
Encoding(x[["surname"]] <- "latin1"
x
# id first_name surname
# 1: 12 Roberto BattiÃ\u008c_n
情况变得更糟。任何提示都非常感谢,谢谢。