Postgres
install.packages("RPostgres") library(dbx) db <- dbxConnect(adapter="postgres", dbname="mydb")
You can also pass user, password, host, port, and url.
RPostgreSQL数据库也是这样操作的
举个例子,完整的连接因该是:
db <- dbxConnect(adapter = "postgres", dbname = " data", host = "test.cls0csjdlwvj.us-west-2.redshift.amazonaws.com", user='milin', password='Milin123', port=5439)
MySQL
install.packages("RMySQL") library(dbx) db <- dbxConnect(adapter="mysql", dbname="mydb")
You can also pass user, password, host, port, and url and Works with RMariaDB as well
SQLite
install.packages("RSQLite") library(dbx) db <- dbxConnect(adapter="sqlite", dbname=":memory:")
SQL Server
install.packages("odbc") library(dbx) db <- dbxConnect(adapter=odbc::odbc(), database="mydb")
You can also pass uid, pwd, server, and port.
Redshift
For Redshift, follow the Postgres instructions.
操作
选择数据
records <- dbxSelect(db, "SELECT * FROM forecasts")
设置参数
dbxSelect(db, "SELECT * FROM forecasts WHERE period = ? AND temperature > ?", params=list("hour", 27))
插入数据
table <- "forecasts"records <- data.frame(temperature=c(32, 25)) dbxInsert(db, table, records)
If you use auto-incrementing ids in Postgres, you can get the ids of newly inserted rows by passing the column name:
dbxInsert(db, table, records, returning=c("id"))
更新数据
records <- data.frame(id=c(1, 2), temperature=c(16, 13))dbxUpdate(db, table, records, where_cols=c("id"))
删除数据
bad_records <- data.frame(id=c(1, 2))dbxDelete(db, table, where=bad_records)
作者:Liam_ml
链接:https://www.jianshu.com/p/b535cf5a0702