Connect to an internal (yet remote) Certe database server using DBI::dbConnect()
, or any other database.
db_connect(driver, ..., print = TRUE)
db_close(conn, ..., print = TRUE)
database driver to use, such as odbc::odbc()
and duckdb::duckdb()
arguments passed on to DBI::dbDisconnect()
a logical to indicate whether info about the connection should be printed
connection to close, such as the output of db_connect()
# create a local duckdb database
db <- db_connect(duckdb::duckdb(), "~/my_duck.db")
#> i Opening connection...
#> OK
db
#> <duckdb_connection 268e0 driver=<duckdb_driver dbdir='/home/runner/my_duck.db' read_only=FALSE bigint=numeric>>
db |> db_write_table("my_iris_table", values = iris)
db |> db_list_tables()
#> [1] "my_iris_table"
db |> db_has_table("my_iris_table")
#> [1] TRUE
if (require(dplyr, warn.conflicts = FALSE)) {
db |>
tbl("my_iris_table") |>
filter(Species == "setosa", Sepal.Width > 3) |>
collect() |>
as_tibble()
}
#> Loading required package: dplyr
#> # A tibble: 42 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.7 3.2 1.3 0.2 setosa
#> 3 4.6 3.1 1.5 0.2 setosa
#> 4 5 3.6 1.4 0.2 setosa
#> 5 5.4 3.9 1.7 0.4 setosa
#> 6 4.6 3.4 1.4 0.3 setosa
#> 7 5 3.4 1.5 0.2 setosa
#> 8 4.9 3.1 1.5 0.1 setosa
#> 9 5.4 3.7 1.5 0.2 setosa
#> 10 4.8 3.4 1.6 0.2 setosa
#> # ℹ 32 more rows
db |> db_drop_table("my_iris_table")
db |> db_list_tables()
#> character(0)
db |> db_close()
#> i Closing connection...
#> OK
# remove the database
unlink("~/my_duck.db")