-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.R
68 lines (40 loc) · 1.99 KB
/
sql.R
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
## load libraries
library(tidyverse)
library(DBI)
con <- DBI::dbConnect(duckdb::duckdb())
dbExecute(con, "CREATE TABLE items ( cut VARCHAR, x INTEGER, y INTEGER)")
DBI::dbCreateTable(con,name = "diamonds",diamonds)
DBI::dbListTables(con)
# insert two items into the table
##manually like this -- you can create a statemen that you continous update
rs <- dbSendStatement(con, "INSERT INTO items VALUES ('jeans', 1,1), ('hammer', 2,1)")
## or you can use place holder value
dat <- diamonds |> select(cut,x,y)
library(tidyverse)
## or you can put places holders and insert lists or unnamed datframe to the place holders
### table is reserved word so you need to escape it
### this updates the databse but just doesn't reutrn any results
rs <- dbSendStatement(con, "INSERT INTO items (cut,x,y) VALUES (?,?,?)",params=dat |> unname())
bSendStatement(con, "INSERT INTO diamonds (carat,cut,color,clarity,depth,\"table\",price,x,y,z) VALUES (?,?,?,?,?,?,?,?,?,?)",params=diamonds |> unname())
## or use a statement placeholder and use dbBind to update
stmt <- dbSendStatement(con, "INSERT INTO items VALUES (?, ?,?)")
dbBind(stmt, list('iphone', 300, 2))
dbBind(stmt, list('test', 3.5, 1))
## another example of dynamically passint args and returns a dataframe
dbGetQuery(con, "SELECT cut,x,y FROM items WHERE y < ? AND cut=?", list(10,'Good'))
## clear statement with dbClearResult
dbClearResult(rs)
#---- dbFetch is used after dbi query to return the query
## dbGetRowcount follows dbFetch to get the total rows that were returned
## dbsendQuery doesn't execute the satement
rs <- dbSendQuery(con, "SELECT* from diamonds ")
rs |> dbFetch()
## dbFetch returns the returned results
dbGetRowCount(rs)
dbGetQuery(con, "SELECT price FROM diamonds WHERE value < ?", list(200))
dbGetQuery(con, "SELECT * FROM diamonds")
dbGetQuery(con, "SELECT * FROM items")
## Can also delete a table with dbSend Statement
dbSendStatement(con, "Drop table items")
dbGetQuery(con,sql("select * from iris limit 100"))
demo("is.things")