dbWriteTable到MySql的UTF8错误

时间:2016-04-19 11:09:13

标签: r rmysql

我正在尝试将导出csv数据写入mysql表。在将数据读入R i时,使用了以下命令

read.csv("mpnov.csv", header = T, stringsAsFactors = F, encoding = "UTF-8")

存储到对象' df'。

可重复的例子如下:

structure(list(ARTICLE_ID = c("4.45e+11", "4.45e+11", "4.45e+11"
), HEADLINE = c("TWEET FROM: DHYANI1811", "TWEET FROM: THE_PARALLEL_ME", 
"TWEET FROM: IMRITIKTHAKUR"), AUTHOR = c("DHYANI1811", "THE_PARALLEL_ME", 
"IMRITIKTHAKUR"), CONTENT = c("@myntra Good Morning If you are born poor its not your mistake, But if you die poor its your mistake. -Bill Gates Good Day", 
"@myntra i have been mailing my issue daily since past week.All i get in reply is an auto generated assurance mail. 1st time pissed wd myntra", 
"@myntra I'm a big Arsenal fan & made a big PUMA collection! ‚_ Shared that collection yesterday. Wanna win... ‚_‚_‚_ #myPUMAcollection"                    
), MEDIA_PROVIDER = c("TWITTER", "TWITTER", "TWITTER")), .Names = c("ARTICLE_ID", 
"HEADLINE", "AUTHOR", "CONTENT", "MEDIA_PROVIDER"), row.names = c(NA, 
3L), class = "data.frame")

列CONTENT具有特殊字符,阻止我将此数据导出到mysql中。我尝试使用以下代码在数据库和表级别将utf8设置为utf8_general_ci。

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

这似乎不起作用。

我在上面的代码中将常规更改为unicode。

ALTER DATABASE databasename CHARACTER SET utfu COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

我试过了SET NAMES cp850

我已经多次删除了数据库和表,并在创建数据库时尝试过:

create database dbname character set utf8;

我还通过将utf8_unicode_ci更改为utf8mb4_unicode_ci

来尝试上述代码

似乎没有任何效果,随着时间的推移,我失去了客观性。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

MySQL的:

show variables like ‘character_set_%’;
SET character_set_database=”utf8”;
SET character_set_server=”utf8”;

似乎dbWriteTable()在Windows上使用utf-8编码存在问题。

你也可以走另一条路:

R:

dbSendQuery(con,'SET NAMES utf8') 
dbSendQuery(conn,"INSERT INTO tablename(fffff) values('xxxxx')");