當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。