【问题标题】:R Programming - Japanese Characters Insert into MySQLR 编程 - 日文字符插入 MySQL
【发布时间】:2014-09-27 19:57:29
【问题描述】:

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

【问题讨论】:

  • con是什么编码?
  • @Roman Luštrik 我在 con. 中使用任何编码。这是我的骗局: con
  • 好的,那么您正在将d 写入表格。在将该对象传递到数据库之前设置Encoding 可能会有所帮助?
  • @Roman Luštrik 你能帮我设置数据帧 d 的编码吗?
  • 现在应该可以在 RMySQL 的开发版本中使用:github.com/rstats-db/RMySQL。请尝试一下,如果您有问题,请告诉我。

标签: mysql r rmysql


【解决方案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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2013-07-05
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多