-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap16.qmd
72 lines (54 loc) · 1.46 KB
/
chap16.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: "必知必会第16课:更新和删除数据"
author: "黄天元"
format: html
editor: visual
---
## 数据库的连接
本次实验会沿用之前保存的文件,因此直接对之前的副本进行连接。
```{r}
library(pacman)
p_load(tidyverse,DBI,RSQLite,fs,dbplyr)
c1= dbConnect(RSQLite::SQLite(), "data/TYSQL_copy1.sqlite")
```
## 基于SQL和tidyverse的数据操作
```{r}
# 16.1
dbExecute(c1,"UPDATE Customers
SET cust_email = '[email protected]'
WHERE cust_id = 1000000005;")
dbExecute(c1,"UPDATE Customers
SET cust_contact = 'Sam Roberts',
cust_email = '[email protected]'
WHERE cust_id = 1000000006;")
dbExecute(c1,"UPDATE Customers
SET cust_email = NULL
WHERE cust_id = 1000000005;")
# 16.2
dbExecute(c1,"DELETE FROM Customers
WHERE cust_id = 1000000006;")
```
注意,函数返回值表示受到这一步操作影响的行数量。
## 练习
- 请使用SQL和R两种方式,解决课后的挑战题。
```{r}
#| echo: false
#| eval: false
# 1
dbExecute(c1,"UPDATE Vendors
SET vend_state = UPPER(vend_state)
WHERE vend_country = 'USA';")
dbExecute(c1,"UPDATE Customers
SET cust_state = UPPER(cust_state)
WHERE cust_country = 'USA';")
# 2
dbGetQuery(c1,"SELECT * FROM Customers
WHERE cust_id = 1000000042;")
dbExecute(c1,"DELETE Customers
WHERE cust_id = 1000000042;")
```
## 关闭数据库
这一步,我们会关闭数据库连接。
```{r}
dbDisconnect(c1)
```