Выборка nvarchar
столбца с японским текстом из Microsoft SQL Server 2016 в R возвращает «????».
Событие попыталось передать кодировку = "UTF-8" в dbconnect, но безуспешно.
Любые указатели будут оценены, спасибо
require(odbc)
# This function takes a string vector and turns it into a list of raw UTF-16LE bytes.
# These will be needed to load into SQL Server
convertToUTF16 <- function(s){
lapply(s, function(x) unlist(iconv(x,from="UTF-8",to="UTF-
16LE",toRaw=TRUE)))
}
# our example data
inputData <- data.frame(ID = c(1,2,3), Char = c("My", "ウェブサイトはダウンしている、もしくはフリーズしています。" ,"Data"))
inputData
# we adjust the column with the UTF-8 strings to instead
# be a list column of UTF-16LE bytes
inputData$Char <- convertToUTF16(inputData$Char)
inputData
#creat a connection to a sql table
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "MSSQL20161",
Database = "MYSCHEMA",
UID = "USER",
PWD = "PASSWORD")
# mention field.types as nvarchar and create/write the table to the database
dbWriteTable(conn = con,
name = "Example",
value = inputData,
append=T,
field.types = c(Char = "NVARCHAR(MAX)")
)
#reading data from mssql table again
readquery="SELECT * FROM Example"
outputdata=sqldf(readquery,conn = con)
outputdata
ID Char
1 1 My
2 2 ?????????????????????????????
3 3 Data
Для полноты вот моя сессия Информация:
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
LC_COLLATE=English_United States.1252
LC_CTYPE=English_United States.1252
LC_MONETARY=English_United States.1252
LC_NUMERIC=C
LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] odbc_1.1.6 sqldf_0.4-11 RSQLite_2.1.1 gsubfn_0.7 proto_1.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.17 digest_0.6.15 chron_2.3-52 DBI_1.0.0 rlang_0.2.1 blob_1.1.1 tools_3.5.0 bit64_0.9-7
[9] bit_1.1-14 hms_0.4.2 compiler_3.5.0 pkgconfig_2.0.1 tcltk_3.5.0 memoise_1.1.0