1.安装DBI和RMySQL包(安装RMySQL时会依赖安装DBI)
install.packages("RMySQL")
2.编写R脚本test.R
# 使用RMySQL操作数据库
# 载入DBI和RMySQL包
library(DBI)
library(RMySQL) # 创建数据库连接
con <- dbConnect(MySQL(),host ="localhost",dbname="test",user="root",password="")
#说明用什么字符集来获取数据库字段
dbGetQuery(con, "SET NAMES gbk") # 验证连接
#print(summary(con)) # SQL查询
results <- dbGetQuery(con,"select * from student") # 断开连接
dbDisconnect(con) # 输出结果集
print(results)
3.查询截图