1、问题
在使用RMySQL包连接MySQL数据库时,出现查询出的汉字显示为“???”号。
2、解决
通过dbSendQuery()设置客户端编码,例如:
dbSendQuery(conn,'SET NAMES gbk')
conn为数据库连接对象。
3、操作过程
> conn <- dbConnect(MySQL(), dbname = "test", username="root", password="",host="127.0.0.1",port=3306)
> dbGetQuery(conn, "select * from t_data limit 0,10")
id_dat sub_station_no sub_station_name sample_time water_temp ph doxy
1 1 5002 ??? 2013-12-31 22:00:00 9 8 8
2 2 5002 ??? 2013-12-31 20:00:00 9 8 8
3 3 5002 ??? 2013-12-31 18:00:00 9 8 8
4 4 5002 ??? 2013-12-31 16:00:00 9 8 8
5 5 5002 ??? 2013-12-31 14:00:00 9 8 8
6 6 5002 ??? 2013-12-31 12:00:00 9 8 8
7 7 5002 ??? 2013-12-31 10:00:00 9 8 8
8 8 5002 ??? 2013-12-31 08:00:00 9 8 8
9 9 5002 ??? 2013-12-31 06:00:00 9 8 8
10 10 5002 ??? 2013-12-31 04:00:00 9 8 8
> dbSendQuery(conn,'SET NAMES uft8')
Error in .local(conn, statement, ...) :
could not run statement: Unknown character set: 'uft8'
> dbSendQuery(conn,'SET NAMES uft-8')
Error in .local(conn, statement, ...) :
could not run statement: Unknown character set: 'uft'
> dbSendQuery(conn,'SET NAMES gbk')
<MySQLResult:0,24,1>
> dbGetQuery(conn, "select * from t_data limit 0,10")
可见,通过设置编码gbk后,结果显示正常(输出略)。