Connector object for DBI connections. This object is used to interact with DBI compliant database backends. See the DBI package for which backends are supported.
Details
Upon garbage collection, the connection will try to disconnect from the database. But it is good practice to call disconnect_cnt when you are done with the connection.
Super class
connector::connector
-> connector_dbi
Active bindings
conn
The DBI connection. Inherits from DBI::DBIConnector
Methods
Method new()
Initialize the connection
Usage
connector_dbi$new(drv, ..., extra_class = NULL)
Arguments
drv
Driver object inheriting from DBI::DBIDriver.
...
Additional arguments passed to
DBI::dbConnect()
.extra_class
character Extra class to assign to the new connector.
Method disconnect_cnt()
Disconnect from the database. See also disconnect_cnt.
Returns
invisible self
.
Method tbl_cnt()
Use dplyr verbs to interact with the remote database table. See also tbl_cnt.
Arguments
name
character Name of the content to read, write, or remove. Typically the table name.
...
Additional arguments passed to the method for the individual connector.
Returns
A dplyr::tbl object.
Examples
# Create DBI connector
cnt <- connector_dbi$new(RSQLite::SQLite(), ":memory:")
cnt
#> <connector_dbi>
#> Inherits from: <connector>
#> Registered methods:
#> • `disconnect_cnt.connector_dbi()`
#> • `list_content_cnt.connector_dbi()`
#> • `read_cnt.connector_dbi()`
#> • `remove_cnt.connector_dbi()`
#> • `tbl_cnt.connector_dbi()`
#> • `write_cnt.connector_dbi()`
#> Specifications:
#> • conn: <SQLiteConnection>
# Write to the database
cnt$write_cnt(iris, "iris")
# Read from the database
cnt$read_cnt("iris") |>
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
# List available tables
cnt$list_content_cnt()
#> [1] "iris"
# Use the connector to run a query
cnt$conn
#> <SQLiteConnection>
#> Path: :memory:
#> Extensions: TRUE
cnt$conn |>
DBI::dbGetQuery("SELECT * FROM iris limit 5")
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
# Use dplyr verbs and collect data
cnt$tbl_cnt("iris") |>
dplyr::filter(Sepal.Length > 7) |>
dplyr::collect()
#> # A tibble: 12 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 7.1 3 5.9 2.1 virginica
#> 2 7.6 3 6.6 2.1 virginica
#> 3 7.3 2.9 6.3 1.8 virginica
#> 4 7.2 3.6 6.1 2.5 virginica
#> 5 7.7 3.8 6.7 2.2 virginica
#> 6 7.7 2.6 6.9 2.3 virginica
#> 7 7.7 2.8 6.7 2 virginica
#> 8 7.2 3.2 6 1.8 virginica
#> 9 7.2 3 5.8 1.6 virginica
#> 10 7.4 2.8 6.1 1.9 virginica
#> 11 7.9 3.8 6.4 2 virginica
#> 12 7.7 3 6.1 2.3 virginica
# Disconnect from the database
cnt$disconnect_cnt()