使用RMySQL的UTF8编码 [英] UTF8 encoding using RMySQL
问题描述
我正在尝试从mysql数据库查询数据,该数据库当然包含一些字符串.对于连接和数据检索,我在R中使用RMySQL,效果很好.除了一件事:我正在检索的字符串似乎不在utf8中.但是我需要这个,因为这些字符串中有一些德语"Umlaute". 当我询问数据库时,
I am trying to query data from a mysql database, which contains some strings, of course. For the connection and data retrieval I am using RMySQL in R, which works fine. Apart from one thing: the strings I am retrieving seem not to be in utf8. But I need this, because I have some german "Umlaute" in these strings. When I ask teh databse, which are its encoding by
dbGetQuery(db, "SHOW VARIABLES LIKE 'character_set_%';")
我得到了想要的答案:
Variable_name Value
1 character_set_client utf8
2 character_set_connection utf8
3 character_set_database utf8
4 character_set_filesystem binary
5 character_set_results utf8
6 character_set_server utf8
7 character_set_system utf8
8 character_sets_dir C:\\Program Files\\MySQL\\MySQL Server 5.7\\share\\charsets\\
但是例如我收到
Andreas Wünsche
代替
Andreas Wünsche
希望有人知道如何处理.如果需要其他信息,只需询问.我可以提供.
Hope that somebody knows how to deal with it. If additonal information is needed, just ask. I can provide it.
推荐答案
我发现有些棘手,但对我有用:
I find something a bit tricky but works for me :
您必须像这样将数据框的col手动定义为utf-8:
you have to manually define the col of your data frame to utf-8 like this :
x <- "Wünsche"
Encoding(x) <- "UTF-8"
x
[1] "Wünsche"
认为您必须对所有字符串向量执行此操作
Think you have to do this to all your strings vector
此处
似乎通过在dbSendQuery()
Take a look here
seems to fix the same problem by adding 'set character set "utf8"'
inside the dbSendQuery()
这篇关于使用RMySQL的UTF8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!