我想插入一个制表符分隔的文件,它用日语和英文字符包含特殊的字符。我正在使用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 States.1252; LC_CTYPE = Japanese_Japan.932; LC_MONETARY = English_United States.1252; LC_NUMERIC = C; LC_TIME = English_United States.1252
尝试的地区:美国,日本。 编码已尝试:UTF-8,16,ASCII。 系统:Windows7 RStudio版本0.98.977 MySQL 5.4.27 CE
答案 0 :(得分:0)
可能您没有正确设置连接的编码。您可以尝试以下方法:
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)
请记住,您必须使用本地编码作为连接的正确编码。我尝试在建议代码的第三行中获取编码,然后根据本地编码设置编码。祝好运!