R编程 - 日文字符插入MySQL

问题描述 投票:0回答:1

我想插入一个制表符分隔的文件,其中包含带有特殊字符的日语和英语字符。我正在使用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

mysql r rmysql
1个回答
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)

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

© www.soinside.com 2019 - 2024. All rights reserved.