R基础学习-- 连接mysql数据库
Posted MemoBlog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R基础学习-- 连接mysql数据库相关的知识,希望对你有一定的参考价值。
测试环境:win10+RStudio
(1)在Console加载两个插件
>install.packages(‘DBI‘)
Installing package into ‘C:/Users/lmei/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/bin/windows/contrib/3.5/DBI_1.0.0.zip‘
Content type ‘application/zip‘ length 884354 bytes (863 KB)
downloaded 863 KB
package ‘DBI’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:UserslmeiAppDataLocalTempRtmp6Fwdfkdownloaded_packages
>install.packages(‘Rmysql‘)
Installing package into ‘C:/Users/lmei/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)trying URL ‘https://cran.rstudio.com/bin/windows/contrib/3.5/RMySQL_0.10.15.zip‘Content type ‘application/zip‘ length 2371644 bytes (2.3 MB)downloaded 2.3 MB
package ‘RMySQL’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:UserslmeiAppDataLocalTempRtmp6Fwdfkdownloaded_packages
(2)编写R测试脚本ConnectMySQL.R,然后点击RUN
library(RMySQL) conn <- dbConnect(MySQL(), dbname = "test", username="root", password="123456",client.flag=CLIENT_MULTI_STATEMENTS) users = dbGetQuery(conn, "SELECT * FROM tb_user") dbDisconnect(conn) print(users)
Console输出
> library(RMySQL) > conn <- dbConnect(MySQL(), dbname = "test", username="root", password="123456",client.flag=CLIENT_MULTI_STATEMENTS) > users = dbGetQuery(conn, "SELECT * FROM tb_user") > dbDisconnect(conn) [1] TRUE > print(users) id name 1 1 Tom
mysql连接读取成功!
以上是关于R基础学习-- 连接mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
R基础学习-- 简单练习(shiny+mysql+barplot)
连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段