MyNixOS website logo
Description

Connect to ODBC Compatible Databases (using the DBI Interface).

A DBI-compatible interface to ODBC databases.

odbc

Project Status: Active – The project has reached a stable, usablestate and is being activelydeveloped. CRAN_Status_Badge R-CMD-check Codecov testcoverage

The goal of the odbc package is to provide a DBI-compliant interface to ODBC drivers. This makes it easy to connect databases such as SQL Server, Oracle, Databricks, and Snowflake.

The odbc package is an alternative to RODBC and RODBCDBI packages, and is typically much faster. See vignette("benchmarks") to learn more.

Overview

The odbc package is one piece of the R interface to databases with support for ODBC:

A diagram containing four boxes with arrows linking each pointing left to right. The boxes read, in order, "R interface," "driver manager," "ODBC driver," and "DBMS." The left-most box, R interface, contains three smaller components, labeled "dbplyr," "DBI," and "odbc."

Support for a given DBMS is provided by an ODBC driver, which defines how to interact with that DBMS using the standardized syntax of ODBC and SQL. Drivers can be downloaded from the DBMS vendor or, if you’re a Posit customer, using the professional drivers.

Drivers are managed by a driver manager, which is responsible for configuring driver locations, and optionally named data sources that describe how to connect to a specific database. Windows is bundled with a driver manager, while MacOS and Linux require installation of unixODBC. Drivers often require some manual configuration; see vignette("setup") for details.

In the R interface, the DBI package provides a front-end while odbc implements a back-end to communicate with the driver manager. The odbc package is built on top of the nanodbc C++ library. To interface with DBMSs using R and odbc:

You might also use the dbplyr package to automatically generate SQL from your dplyr code.

Installation

Install the latest release of odbc from CRAN with the following code:

install.packages("odbc")

To get a bug fix or to use a feature from the development version, you can install the development version of odbc from GitHub:

# install.packages("pak")
pak::pak("r-dbi/odbc")

Usage

To use odbc, begin by creating a database connection, which might look something like this:

library(DBI)

con <- dbConnect(
  odbc::odbc(),
  driver = "SQL Server",
  server = "my-server",
  database = "my-database",
  uid = "my-username",
  pwd = rstudioapi::askForPassword("Database password")
)

(See vignette("setup") for examples of connecting to a variety of databases.)

dbListTables() is used for listing all existing tables in a database.

dbListTables(con)

dbReadTable() will read a full table into an R data.frame().

data <- dbReadTable(con, "flights")

dbWriteTable() will write an R data.frame() to an SQL table.

dbWriteTable(con, "iris", iris)

dbGetQuery() will submit a SQL query and fetch the results:

df <- dbGetQuery(
  con,
  "SELECT flight, tailnum, origin FROM flights ORDER BY origin"
)

It is also possible to submit the query and fetch separately with dbSendQuery() and dbFetch(). This allows you to use the n argument to dbFetch() to iterate over results that would otherwise be too large to fit in memory.

Metadata

Version

1.5.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows