这些是 dplyr rows_insert()
、 rows_append()
、 rows_update()
、 rows_patch()
、 rows_upsert()
和 rows_delete()
泛型的方法。
当 in_place = TRUE
时,这些动词不会生成 SELECT
查询,而是使用 INSERT
、 UPDATE
或 DELETE
运算符直接修改底层数据。这将要求您具有对数据库的写访问权限:连接需要插入、修改或删除行的权限,但不能更改表的结构。
默认值 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
-
给出关键列的未命名字符向量。键列必须同时存在于
x
和y
中。键通常唯一标识每一行,但这仅在使用rows_update()
、rows_patch()
或rows_upsert()
时对y
的键值强制执行。默认情况下,我们使用
y
中的第一列,因为第一列是放置标识符变量的合理位置。 - ...
-
传递给方法的其他参数。
- conflict
-
对于
rows_insert()
,y
中的键与x
中的键冲突应该如何处理?如果y
中的键已存在于x
中,则会发生冲突。之一:
-
数据库表不支持默认值
"error"
。要获得相同的行为,请在by
列上添加唯一索引并使用rows_append()
。 -
"ignore"
将忽略y
中与x
中的键冲突的行。
-
- copy
-
如果
x
和y
不是来自同一个数据源,并且copy
是TRUE
,则y
将被复制到与x
相同的源中。这允许您跨 src 连接表,但这是一项潜在昂贵的操作,因此您必须选择它。 - in_place
-
是否应该就地修改
x
?如果FALSE
将生成返回修改后的表的SELECT
查询;如果TRUE
将使用 DML 操作修改基础表(INSERT
、UPDATE
、DELETE
或类似操作)。 - returning
-
要返回的列。有关详细信息,请参阅
get_returned_rows()
。 - method
-
指定要使用的方法的字符串。这仅与
in_place = TRUE
相关。 - unmatched
-
对于
rows_update()
、rows_patch()
和rows_delete()
,应如何处理y
中与x
中的键不匹配的键?之一:
-
数据库表不支持默认值
"error"
。在y
的by
列上添加外键约束,让数据库为您检查此行为。 -
"ignore"
将忽略y
中的行,这些行的键与x
中的键不匹配。
-
值
修改数据的新tbl_lazy
。使用 in_place = FALSE
,结果是一个可见打印的惰性查询,因为此操作的目的是预览结果。对于 in_place = TRUE
, x
会被无形地返回,因为此操作的目的是修改 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 dbplyr replace_na.tbl_lazy 将 NA 替换为指定值
- R dbplyr remote_name 有关远程表的元数据
- R dbplyr backend-teradata 后端:Teradata
- R dbplyr escape 转义/引用字符串。
- R dbplyr expand.tbl_lazy 扩展 SQL 表以包含所有可能的值组合
- R dbplyr distinct.tbl_lazy 子集不同/唯一行
- R dbplyr backend-sqlite 后端:SQLite
- R dbplyr pivot_wider.tbl_lazy 将数据从长轴转向宽轴
- R dbplyr build_sql 构建 SQL 字符串。
- R dbplyr mutate.tbl_lazy 创建、修改和删除列
- R dbplyr collapse.tbl_sql 计算查询的结果
- R dbplyr sql_expr 从 R 表达式生成 SQL
- R dbplyr get_returned_rows 提取并检查返回的行
- R dbplyr dbplyr_uncount “计数”数据库表
- R dbplyr count.tbl_lazy 按组计数观察值
- R dbplyr backend-odbc 后端:ODBC
- R dbplyr head.tbl_lazy 对第一行进行子集化
- R dbplyr db-quote SQL 转义/引用泛型
- R dbplyr copy_inline 在 dbplyr 查询中使用本地 DataFrame
- R dbplyr backend-oracle 后端:甲骨文
- R dbplyr backend-snowflake 后端:雪花
- R dbplyr lahman 缓存并检索 Lahman 棒球数据库的 src_sqlite。
- R dbplyr backend-redshift 后端:红移
- R dbplyr partial_eval 部分评估表达式。
- R dbplyr group_by.tbl_lazy 按一个或多个变量分组
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Edit individual rows in the underlying database table。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。