R 连接 mysql
方式一
library(RMySQL)
con <- dbConnect(MySQL(),
user='root',
password='52332580',
dbname='douban',
host="127.0.0.1")
dbListTables(con)
# 设置编码格式
dbSendQuery(con, "SET NAMES utf8")
# 查询数据
query <- "SELECT * FROM movie"
# 提取数据
data <- dbGetQuery(con, query)
# 关闭连接
dbDisconnect(con)
方式二
library(tidyverse)
my_db <- src_mysql(dbname="douban",host = "127.0.0.1",port = 3306,user = "root",password = "52332580")
my_tbl <- tbl(my_db,"movie")
my_tbl%>%select(title,url)
乱码解决方式如下:
1、sudo vim /etc/mysql/my.cnf
添加:
[client]
default-character-set=utf8
2、sudo service mysqld restart
library(RMySQL)
con <- dbConnect(MySQL(),
user='root',
password='52332580',
dbname='douban',
host="127.0.0.1")
dbListTables(con)
# 设置编码格式
dbSendQuery(con, "SET NAMES utf8")
# 查询数据
query <- "SELECT * FROM movie"
# 提取数据
data <- dbGetQuery(con, query)
# 关闭连接
dbDisconnect(con)
方式二
library(tidyverse)
my_db <- src_mysql(dbname="douban",host = "127.0.0.1",port = 3306,user = "root",password = "52332580")
my_tbl <- tbl(my_db,"movie")
my_tbl%>%select(title,url)
乱码解决方式如下:
1、sudo vim /etc/mysql/my.cnf
添加:
[client]
default-character-set=utf8
2、sudo service mysqld restart
评论
发表评论