R

ADBC is available in R through the adbcdrivermanager package, which provides a low-level interface along with the read_adbc(), write_adbc(), and execute_adbc() helpers for quickly interacting with an ADBC connection or database.

Installation

install.packages(c("adbcdrivermanager", "arrow", "tibble"))

Installing Drivers

See Installing Drivers for instructions on installing ADBC drivers for the database you want to connect to. For the example below, you could install dbc and then install the SQLite driver with:

dbc install sqlite

Basic Example

This example demonstrates connecting to SQLite, writing a table, querying it, and cleaning up.

library(adbcdrivermanager)

# Create a driver instance
drv <- adbc_driver("sqlite")

# Initialize the database
db <- adbc_database_init(drv, uri = ":memory:")

# Create a connection
con <- adbc_connection_init(db)

# Write a table
mtcars |>
  write_adbc(con, "mtcars")

# Query it
con |>
  read_adbc("SELECT * FROM mtcars") |>
  tibble::as_tibble()

# Clean up
con |>
  execute_adbc("DROP TABLE mtcars")
adbc_connection_release(con)
adbc_database_release(db)

Working with Arrow Data

ADBC natively returns Arrow data, which you can work with directly:

# Get results as an Arrow table
arrow_table <- con |>
  read_adbc("SELECT * FROM mtcars") |>
  arrow::as_arrow_table()

# For large result sets, use a record batch reader
reader <- con |>
  read_adbc("SELECT * FROM large_table") |>
  arrow::as_record_batch_reader()

# Process in chunks
while (!is.null(batch <- reader$read_next_batch())) {
  # Process batch
  print(nrow(batch))
}

Parameterized Queries

You can use parameterized queries for safe SQL execution:

stmt <- adbc_statement_init(con)
adbc_statement_set_sql_query(stmt, "DELETE FROM mtcars WHERE cyl = ?")
adbc_statement_bind(stmt, data.frame(8L))
adbc_statement_execute_query(stmt)

Package Documentation

The adbcdrivermanager package is the R client library, and provides the driver manager used in the examples above:

A few drivers are also built as R packages and distributed on CRAN:

In general, though, we recommend installing drivers as shared libraries (see Installing Drivers), which works with far more databases than are available as R packages.

Next Steps