Debian Codegraph @

man/with_db_connection.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/db.R
3 \name{with_db_connection}
4 \alias{with_db_connection}
5 \alias{local_db_connection}
6 \title{DBMS Connections which disconnect themselves.}
7 \usage{
8 with_db_connection(con, code)
9
10 local_db_connection(con, .local_envir = parent.frame())
11 }
12 \arguments{
13 \item{con}{For \code{with_db_connection()} a named list with the connection(s) to
14 create. For \code{local_db_connection()} the code to create a single connection,
15 which is then returned.}
16
17 \item{code}{\code{[any]}\cr Code to execute in the temporary environment}
18
19 \item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.}
20 }
21 \value{
22 \code{[any]}\cr The results of the evaluation of the \code{code}
23 argument.
24 }
25 \description{
26 Connections to Database Management Systems which automatically disconnect. In
27 particular connections which are created with \code{DBI::dbConnect()} and closed
28 with \code{DBI::dbDisconnect()}.
29 }
30 \examples{
31 db <- tempfile()
32 with_db_connection(
33 list(con = DBI::dbConnect(RSQLite::SQLite(), db)), {
34 DBI::dbWriteTable(con, "mtcars", mtcars)
35 })
36
37 head_db_table <- function(...) {
38 con <- local_db_connection(DBI::dbConnect(RSQLite::SQLite(), db))
39 head(DBI::dbReadTable(con, "mtcars"), ...)
40 }
41 head_db_table()
42 unlink(db)
43 }
44 \seealso{
45 \code{\link{withr}} for examples
46 }