提问者:小点点

R编程-日语字符插入MySQL


我想插入一个制表符分隔的文件,它使用特殊字符连接日语和英语字符。我正在使用RMySQL做的是。我尝试给出以下错误的解决方案之一:

dbWriteTable(con, "japan_test2", d, append = T, row.names=FALSE);
Error in mysqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '˜¨å¤œã®ã‚³ãƒ³_ text)' at line 3)
In addition: Warning message:
In strsplit(msg, "\n") : input string 1 is invalid in this locale
[1] FALSE
Warning message:
In mysqlWriteTable(conn, name, value, ...) :
could not create table: aborting mysqlWriteTable

当前区域设置:LC_COLLATE=English_United州;LC_CTYPE=Japanese_Japan.932;LC_MONETARY=English_United州;LC_NUMERIC=C;LC_TIME=English_United州。

尝试区域设置:US,日语。尝试编码:UTF-8,16,ASCII。系统:Windows7 RStudio版本0.98.977 MySQL 5.4.27CE


共1个答案

匿名用户

可能您没有正确设置连接的编码。你可以试试这个:

con <- dbConnect(MySQL(), user=user, password=password,dbname=dbname, host=host, port=port)
# With the next line I try to get the right encoding (it works for Spanish keyboards)
encoding <- if(grepl(pattern = 'utf8|utf-8',x = Sys.getlocale(),ignore.case = T)) 'utf8' else 'latin1'
dbGetQuery(con,paste("SET names",encoding))
dbGetQuery(con,paste0("SET SESSION character_set_server=",encoding))
dbGetQuery(con,paste0("SET SESSION character_set_database=",encoding))
dbWriteTable( con, value = dfr, name = table, append = TRUE, row.names = FALSE )
dbDisconnect(con)

请记住,您必须使用本地编码作为连接的正确编码。我尝试在提议的代码的第三行中获取我的编码,然后根据我的本地编码设置编码。祝你好运!