在 dbWriteTable 到 MySQL 中获取 UTF8 错误

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

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

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;

这好像不行。

我在上面的代码中将 general 更改为 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

来尝试上面的代码

似乎没有任何效果。接下来我可以尝试什么?

r rmysql
1个回答
0
投票

MySQL:

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

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

你也可以走另一条路:

R:

dbSendQuery(con,'SET NAMES utf8') 
dbSendQuery(conn,"INSERT INTO tablename(fffff) values('xxxxx')");
© www.soinside.com 2019 - 2024. All rights reserved.