# 使用RMySQL操作数据库
# 载入DBI和RMySQL包
library(DBI)
library(RMySQL)
# 创建数据库连接
con <- dbConnect(MySQL(),host ="localhost",dbname="test",user="root",password="123456")
#说明用什么字符集来获取数据库字段
dbGetQuery(con, "SET NAMES gbk")
# SQL查询
# 获取结果
results <- dbGetQuery(con,'select `HS整车控制器故障代码""` from raja_7j05b where `HS整车控制器故障代码""`!=0')
# 获取字符长度
len <- as.integer(unlist(dbGetQuery(con,'select count(*) from raja_7j05b where `HS整车控制器故障代码""`!=0')))
# 断开连接
dbDisconnect(con)
# 构建相应长度向量
x <- c(1:len)#;print(class(x))
# 将列表转化为向量
y <- as.integer(unlist(results))#;print(class(y))
# 绘制散点图
plot(x,y)
![]()