acquireConn | R Documentation |
Acquire a (possibly cached) SQL file connection given it's path.
acquireConn(path, dbtype = NULL)
releaseConn(path)
path |
String containing a path to a SQL file. |
dbtype |
String containing the SQL database type (case insensitive). Supported types are "SQLite" and "DuckDB". |
acquireConn
will cache the DBIConnection object in
the current R session to avoid repeated initialization. This
improves efficiency for repeated calls, e.g., when creating a
DataFrame with multiple columns from the same SQL
table. The cached DBIConnection for any given path
can
be deleted by calling releaseConn
for the same
path
.
For acquireConn
, a DBIConnection with backends of
SQLite or DuckDB, which are identical to that returned by
DBI::dbConnect(RSQLite::SQLite(), path)
or
DBI::dbConnect(duckdb::duckdb(), path)
.
For releaseConn
, any existing DBIConnection for the
path
is disconnected and cleared from cache, and NULL
is invisibly returned. If path=NULL
, all cached connections
are removed.
Qian Liu
###########
## SQLite
###########
## Mocking up a file
tf <- tempfile()
on.exit(unlink(tf))
con <- DBI::dbConnect(RSQLite::SQLite(), tf)
DBI::dbWriteTable(con, "mtcars", mtcars)
DBI::dbDisconnect(con)
## Acquire or release connection
con <- acquireConn(tf, dbtype = "SQLite")
acquireConn(tf, dbtype = "SQLite") # just re-uses the cache
releaseConn(tf) # clears the cache
###########
## DuckDB
###########
tf1 <- tempfile()
on.exit(unlist(tf1))
con <- DBI::dbConnect(duckdb::duckdb(), tf1)
DBI::dbWriteTable(con, "mtcars", mtcars)
DBI::dbDisconnect(con)
con <- acquireConn(tf1, dbtype = "DuckDB")
releaseConn(tf1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.