继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

A fast, easy-to-use database library for R

慕的地10843
关注TA
已关注
手记 1081
粉丝 200
获赞 962

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


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP