当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R dbplyr rows-db 编辑基础数据库表中的各个行


这些是 dplyr rows_insert()rows_append()rows_update()rows_patch()rows_upsert()rows_delete() 泛型的方法。

in_place = TRUE 时,这些动词不会生成 SELECT 查询,而是使用 INSERTUPDATEDELETE 运算符直接修改底层数据。这将要求您具有对数据库的写访问权限:连接需要插入、修改或删除行的权限,但不能更改表的结构。

默认值 in_place = FALSE 生成等效的惰性表(使用 SELECT 查询),允许预览结果,而无需实际修改数据库上的基础表。

用法

# S3 method for tbl_lazy
rows_insert(
  x,
  y,
  by = NULL,
  ...,
  conflict = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE,
  returning = NULL,
  method = NULL
)

# S3 method for tbl_lazy
rows_append(x, y, ..., copy = FALSE, in_place = FALSE, returning = NULL)

# S3 method for tbl_lazy
rows_update(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE,
  returning = NULL
)

# S3 method for tbl_lazy
rows_patch(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE,
  returning = NULL
)

# S3 method for tbl_lazy
rows_upsert(
  x,
  y,
  by = NULL,
  ...,
  copy = FALSE,
  in_place = FALSE,
  returning = NULL,
  method = NULL
)

# S3 method for tbl_lazy
rows_delete(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE,
  returning = NULL
)

参数

x

一张懒人的表格。对于 in_place = TRUE ,这必须是用 tbl()compute() 实例化的表,而不是惰性查询。 remote_name()函数用于确定要更新的表的名称。

y

惰性表、 DataFrame 或 DataFrame 扩展(例如 tibble)。

by

给出关键列的未命名字符向量。键列必须同时存在于 xy 中。键通常唯一标识每一行,但这仅在使用 rows_update()rows_patch()rows_upsert() 时对 y 的键值强制执行。

默认情况下,我们使用 y 中的第一列,因为第一列是放置标识符变量的合理位置。

...

传递给方法的其他参数。

conflict

对于rows_insert()y中的键与x中的键冲突应该如何处理?如果 y 中的键已存在于 x 中,则会发生冲突。

之一:

  • 数据库表不支持默认值"error"。要获得相同的行为,请在 by 列上添加唯一索引并使用 rows_append()

  • "ignore" 将忽略 y 中与 x 中的键冲突的行。

copy

如果 xy 不是来自同一个数据源,并且 copyTRUE ,则 y 将被复制到与 x 相同的源中。这允许您跨 src 连接表,但这是一项潜在昂贵的操作,因此您必须选择它。

in_place

是否应该就地修改x?如果FALSE将生成返回修改后的表的SELECT查询;如果 TRUE 将使用 DML 操作修改基础表( INSERTUPDATEDELETE 或类似操作)。

returning

要返回的列。有关详细信息,请参阅get_returned_rows()

method

指定要使用的方法的字符串。这仅与 in_place = TRUE 相关。

unmatched

对于 rows_update()rows_patch()rows_delete() ,应如何处理 y 中与 x 中的键不匹配的键?

之一:

  • 数据库表不支持默认值"error"。在 yby 列上添加外键约束,让数据库为您检查此行为。

  • "ignore" 将忽略 y 中的行,这些行的键与 x 中的键不匹配。

修改数据的新tbl_lazy。使用 in_place = FALSE ,结果是一个可见打印的惰性查询,因为此操作的目的是预览结果。对于 in_place = TRUEx 会被无形地返回,因为此操作的目的是修改 x 后面表中的行的副作用。

例子

library(dplyr)

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbExecute(con, "CREATE TABLE Ponies (
   id INTEGER PRIMARY KEY AUTOINCREMENT,
   name TEXT,
   cutie_mark TEXT
)")
#> [1] 0

ponies <- tbl(con, "Ponies")

applejack <- copy_inline(con, data.frame(
  name = "Apple Jack",
  cutie_mark = "three apples"
))

# The default behavior is to generate a SELECT query
rows_insert(ponies, applejack, conflict = "ignore")
#> Matching, by = "name"
#> # Source:   SQL [1 x 3]
#> # Database: sqlite 3.41.2 [:memory:]
#>   id    name       cutie_mark  
#>   <lgl> <chr>      <chr>       
#> 1 NA    Apple Jack three apples
# And the original table is left unchanged:
ponies
#> # Source:   table<Ponies> [0 x 3]
#> # Database: sqlite 3.41.2 [:memory:]
#> # ℹ 3 variables: id <int>, name <chr>, cutie_mark <chr>

# You can also choose to modify the table with in_place = TRUE:
rows_insert(ponies, applejack, conflict = "ignore", in_place = TRUE)
#> Matching, by = "name"
# In this case `rows_insert()` returns nothing and the underlying
# data is modified
ponies
#> # Source:   table<Ponies> [1 x 3]
#> # Database: sqlite 3.41.2 [:memory:]
#>      id name       cutie_mark  
#>   <int> <chr>      <chr>       
#> 1     1 Apple Jack three apples
源代码:R/rows.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Edit individual rows in the underlying database table。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。