-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.R
More file actions
52 lines (34 loc) · 1.06 KB
/
SQL.R
File metadata and controls
52 lines (34 loc) · 1.06 KB
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
#Connection asking with password
con <- DBI::dbConnect(RMySQL::MySQL(),
host = "localhost",
user = "root",
password = rstudioapi::askForPassword("Database password")
)
#Connection asking without password
con <- DBI::dbConnect(RMySQL::MySQL(),
dbname = "ranu_database2",
host = "localhost",
user = "root",
password = "abc123"
)
#connect with table and extract city
tbl(con,"Persons") %>% select(City) %>% show_query()
?copy_to
#copy data from dataframe to SQL
copy_to(con, nycflights13::flights, "flights",
temporary = FALSE,
indexes = list(
c("year", "month", "day"),
"carrier",
"tailnum",
"dest"
)
)
tbl(con,"flights") %>% select(carrier) %>% show_query()
tbl(con,"flights") %>% select(year:day, dep_delay, arr_delay)
tbl(con,"flights") %>% filter(dep_delay > 240)
tbl(con,"flights") %>% group_by(dest) %>%
summarise(delay = mean(dep_time)) %>% show_query()
#Disconnection
library(DBI)
dbDisconnect(con)