尝试将数据从R写入MySQL时编码问题

时间:2018-05-07 16:14:24

标签: mysql r encoding utf-8 rmysql

我的数据包含德语变音符号等特殊字符。

p=structure(list(ppl_code = c(992621L, 992381L, 992136L, 991989L, 
991898L, 991759L, 991681L, 991593L, 991294L, 991036L, 990934L, 
990751L, 990535L, 990411L, 990182L, 989507L), proj_name = c("klo", 
"Dalbygda", "Oosterhorn", "Hån", "Yatir", "Montigny la Cour", 
"Valle Hermoso", "Acciona Honawad - 120 MW", "Apfeltrang", "RiaBlades", 
"General Acha", "Lindau-Böhlitz", "Apfeltrang", "Alcazar Round 2", 
"Peckelsheim", "Linnich 3")), .Names = c("ppl_code", "proj_name"
), row.names = 15:30, class = "data.frame")

当我尝试将其写入MySQL数据库时:

conn <- dbConnect(
       drv = RMySQL::MySQL(),
       dbname = "mydb",
       host = "#####",
       username = "#####",
       password = "#####")

dbWriteTable(conn, value = p, name = "MyTable",row.names=FALSE)

我收到了编码错误:

could not run statement: Invalid utf8 character string: 'Lindau-B'

我已经查看了几个有关此问题的帖子,例如herehere,但它们都是一般解释,没有明确的解决方案! 任何人都可以帮我解决一个可以解决这个问题的清晰查询吗?

1 个答案:

答案 0 :(得分:0)

您需要宣布正在使用UTF-8。

Tool -> Global Options -> Code -> Saving and put UTF-8

rs <- dbSendQuery(con, 'SET NAMES utf8')