[問題類型]: 想透過Rmysql將資料匯入mysql,但中文字會變成亂碼。
[軟體熟悉度]:
使用者
[問題敘述]:
透過Rmysql連結mysql,寫入中文或非英數字串,會變成亂碼,
試過一些網路上的解法,但好像沒有辦法改變...
且網路上的資源英文居多,但英文就比較不容易發生這種問題。
直接在用phpmyadmin讀寫mysql的中英文都正常。
環境是windows 10 64位元
R 3.3.2 。
麻煩高手賜教...謝謝!
[程式範例]:
library(DBI)
library(RMySQL)
library(dplyr)
con <- dbConnect(MySQL(), user="root", password="test", dbname="test",
host="localhost")
> dbSendQuery(con,"SET NAMES utf8")
<MySQLResult:0,0,13>
> df <- data.frame(aa = enc2utf8("台北"), bb = enc2utf8("台中")
, cc=enc2utf8("ののののの") , dd="FF" ,ee="GG")
> dbWriteTable(con, name = "new10", value = df, overwrite = FALSE,
row.names = FALSE, append = T, fileEncoding="UTF-8")
[1] TRUE
> dbReadTable(con, 'new10')
aa bb cc dd ee
1 瞼x瞼_ 瞼x瞻瞻 <U+306E><U+306E><U+306E><U+306E><U+306E> FF GG
## 如上,寫入的中文跟日文都變成亂碼了..
## 附上一些其他資訊如下:
> dbGetQuery(con,"show variables like 'character_set_%'")
Variable_name Value
1 character_set_client utf8
2 character_set_connection utf8
3 character_set_databas latin1
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:\\Bitnami\\wampstack-5.6.30-1\\mysql\\share\\charsets\\
[環境敘述]:
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale: [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C [5] LC_TIME=Chinese (Traditional)_Taiwan.950 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1]
RMySQL_0.10.11 DBI_0.5-1
loaded via a namespace (and not attached): [1] tools_3.3.2
[關鍵字]: Rmysql mysql