odbc {odbc} | R Documentation |
Connect to a database via an ODBC driver
Description
The dbConnect()
method documented here is invoked when DBI::dbConnect()
is called with the first argument odbc()
. Connecting to a database via
an ODBC driver is likely the first step in analyzing data using the odbc
package; for an overview of package concepts, see the Overview section
below.
Usage
odbc()
## S4 method for signature 'OdbcDriver'
dbConnect(
drv,
dsn = NULL,
...,
timezone = "UTC",
timezone_out = "UTC",
encoding = "",
bigint = c("integer64", "integer", "numeric", "character"),
timeout = 10,
driver = NULL,
server = NULL,
database = NULL,
uid = NULL,
pwd = NULL,
dbms.name = NULL,
attributes = NULL,
interruptible = getOption("odbc.interruptible", interactive()),
.connection_string = NULL
)
Arguments
drv |
An |
dsn |
The data source name. For currently available options, see the
|
... |
Additional ODBC keywords. These will be joined with the other arguments to form the final connection string. Note that ODBC parameter names are case-insensitive so that (e.g.) Any values containing a leading or trailing space, a |
timezone |
The server time zone. Useful if the database has an internal
timezone that is not 'UTC'. If the database is in your local timezone,
set this argument to |
timezone_out |
The time zone returned to R. If you want to display
datetime values in the local timezone, set to |
encoding |
The text encoding used on the Database. If the database is
not using UTF-8 you will need to set the encoding to get accurate
re-encoding. See |
bigint |
The R type that |
timeout |
Time in seconds to timeout the connection attempt. Setting a
timeout of |
driver |
The ODBC driver name or a path to a driver. For currently
available options, see the |
server |
The server hostname. Some drivers use |
database |
The database on the server. Not required when configured for
the supplied |
uid |
The user identifier. Some drivers use |
pwd |
The password. Some drivers use |
dbms.name |
The database management system name. This should normally
be queried automatically by the ODBC driver. This name is used as the class
name for the OdbcConnection object returned from |
attributes |
A list of connection attributes that are passed prior to the connection being established. See ConnectionAttributes. |
interruptible |
Logical. If |
.connection_string |
A complete connection string, useful if you are copy pasting it from another source. If this argument is used, any additional arguments will be appended to this string. |
Connection strings
Internally, dbConnect()
creates a connection string using the supplied
arguments. Connection string keywords are driver-dependent; the arguments
documented here are common, but some drivers may not accept them.
Alternatively to configuring DSNs and driver names with the driver manager,
you can pass a complete connection string directly as the
.connection_string
argument.
The Connection Strings Reference is a
useful resource that has example connection strings for a large variety of
databases.
Overview
The odbc package is one piece of the R interface to databases with support for ODBC:
The package supports any Database Management System (DBMS) with ODBC support. 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. To manage information about each driver and the data sources they provide access to, our computers use a driver manager. Windows is bundled with a driver manager, while MacOS and Linux require installation of one; this package supports the unixODBC driver manager.
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.
Interfacing with DBMSs using R and odbc involves three high-level steps:
-
Configure drivers and data sources: the functions
odbcListDrivers()
andodbcListDataSources()
help to interface with the driver manager. -
Connect to a database: The
dbConnect()
function, called with the first argument odbc(), connects to a database using the specified ODBC driver to create a connection object. -
Interface with connections: The resulting connection object can be passed to various functions to retrieve information on database structure (
dbListTables()
), iteratively develop queries (dbSendQuery()
,dbColumnInfo()
), and query data objects (dbFetch()
).
Learn more
To learn more about databases:
-
"Best Practices in Working with Databases" documents how to use the odbc package with various popular databases.
-
The pyodbc "Drivers and Driver Managers" Wiki provides further context on drivers and driver managers.
-
Microsoft's "Introduction to ODBC" is a thorough resource on the ODBC interface.