A fast, easy-to-use database lib

作者: Liam_ml | 来源:发表于2018-10-13 00:52 被阅读9次

这是一个新包,2018-09-05发布的,挺好用的,在这里介绍。

Installation

install.packages("dbx")

根据你的数据库不一样,还需要下列包

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)

详细请参见github:https://github.com/ankane/dbx

相关文章

网友评论

    本文标题:A fast, easy-to-use database lib

    本文链接:https://www.haomeiwen.com/subject/auvnaftx.html