2015-11-27 44 views
4

我正嘗試將數據庫導入到R中以轉換並重新加載到另一個數據庫中。我拉我的一套從RDS MySQL搭配:無法更改R中的數據幀中的編碼

con <- dbConnect(MySQL(), 
        user = 'user', 
        password = 'password', 
        host = 'url', 
        dbname='dbName') 

sqlcmd = paste("SELECT * FROM dbName.`users`"); 

contentTable = dbGetQuery(con,sqlcmd); 

contentTable["first_name"] 

其中網我這個不幸的輸出

first_name 
1  Sergio 
2  Sara 
3 J\xfalia 
4 Tatiana 
5  Paula 

我的問題是,第三個名字應該回來爲朱莉婭。此問題也發生在其他行中。

我的語言環境設置如下。

> Sys.getlocale() 
[1] "pt_PT.UTF-8/pt_PT.UTF-8/pt_PT.UTF-8/C/pt_PT.UTF-8/en_US.UTF-8" 

和服務器的默認字符是

# Variable_name, Value 
'character_set_client', 'utf8' 
'character_set_connection', 'utf8' 
'character_set_database', 'utf8' 
'character_set_filesystem', 'binary' 
'character_set_results', 'utf8' 
'character_set_server', 'latin1' 
'character_set_system', 'utf8' 
'character_sets_dir', '/rdsdbbin/oscar-5.6.10a.14.15/share/charsets/' 

我有點失去了和即將切換到Python /熊貓(這給了我正確的字符,但我有一個學習的曲線來面對我想要做的下一步)。任何想法現在要做什麼?

UPDATE1:

SHOW CREATE TABLE users; 
CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(255) NOT NULL, 
    `last_name` varchar(255) NOT NULL, 
    `email` varchar(255) NOT NULL, 
    `birthday` date DEFAULT NULL, 
    `age` int(11) DEFAULT NULL, 
    `city` varchar(255) DEFAULT NULL, 
    `state` varchar(255) DEFAULT NULL, 
    `country` varchar(255) DEFAULT NULL, 
    `beer_points` int(11) DEFAULT NULL, 
    `access_token` text, 
    `created_at` datetime NOT NULL, 
    `updated_at` datetime NOT NULL, 
    `profile_picture_file_name` varchar(255) DEFAULT NULL, 
    `profile_picture_content_type` varchar(255) DEFAULT NULL, 
    `profile_picture_file_size` int(11) DEFAULT NULL, 
    `profile_picture_updated_at` datetime DEFAULT NULL, 
    `role` varchar(255) DEFAULT NULL, 
    `password_digest` varchar(255) DEFAULT NULL, 
    `gender` varchar(255) DEFAULT NULL, 
    `share_code` varchar(255) DEFAULT NULL, 
    `privacy_enabled` tinyint(1) DEFAULT '0', 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `index_users_on_email` (`email`), 
    KEY `index_users_on_role` (`role`) 
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=latin1 
+0

複製和粘貼朱莉婭後,我無法重現您的問題,並插入名字變成一個MySQL數據庫。在使用RMySQL遠程連接R以進行數據幀導入後,Júlia導入得很好。 Júlia(帶口音)在MySQL中是如此顯示的嗎?另外,檢查數據庫表的字符集:'SHOW CREATE TABLE users'。 – Parfait

+0

@Parfait它在MySQL中顯示爲Júlia(Workbench和Sequel Pro)。我添加了你問的信息。謝謝! – brunoban

+0

它可能是你的UTF-8系統語言環境,我相信這是葡萄牙語,並嘗試閱讀latin1。看看管理你的[sys.locale()](http://stackoverflow.com/questions/23324872/rstudio-not-picking-the-encoding-im-telling-it-to-use-when-reading-a-文件)和/或更改[它](http://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r-in-rstudio)。 – Parfait

回答

1

此代碼可能是你的問題非常有用:

con <- dbConnect(MySQL(), 
       user = 'user', 
       password = 'password', 
       host = 'url', 
       dbname='dbName') 
m <- dbGetQuery(con, "SET NAMES 'latin1'") 
sqlcmd <- paste("SELECT * FROM dbName.`users`"); 
result <- dbGetQuery(con, sqlcmd) 
dbDisconnect(con)