这就是我试图将mysql连接到r的方式。
db <- dbConnect(MySQL(), user='username', password='pwd',dbname=dbx, host = 'local', port = 3306)
但我得到了一个错误:.local(drv,…)中出错:无法连接到数据库:错误:无法加载插件缓存\u sha2 \u密码:找不到指定的模块。怎么办?
mf98qq941#
您可以创建一个函数来检索查询。
library(RMySQL)sqlQuery <- function (query) { # creating DB connection object with RMysql package DB <- dbConnect(MySQL(), user="user", password="password", dbname="databaseName", host="host") # close db connection after function call exits on.exit(dbDisconnect(DB)) # send Query to obtain result set rs <- dbSendQuery(DB, query) # get elements from result sets and convert to dataframe result <- fetch(rs, -1) # return the dataframe return(result)}
library(RMySQL)
sqlQuery <- function (query) {
# creating DB connection object with RMysql package
DB <- dbConnect(MySQL(), user="user", password="password",
dbname="databaseName", host="host")
# close db connection after function call exits
on.exit(dbDisconnect(DB))
# send Query to obtain result set
rs <- dbSendQuery(DB, query)
# get elements from result sets and convert to dataframe
result <- fetch(rs, -1)
# return the dataframe
return(result)
}
然后就是:
new_dataframe <- sqlQuery("SELECT * from table")
希望有帮助
1条答案
按热度按时间mf98qq941#
您可以创建一个函数来检索查询。
然后就是:
希望有帮助