Create a connection to a DBMS

Connect to a DBMS going through the appropriate authorization procedure.

interface, database
dbConnect(drv, ...)
  dbDisconnect(conn, ...)
an object that inherits from DBIDriver, a character string specifying the DBMS driver, e.g., "RPgSQL", "ROracle", "Informix", or possibly another dbConnect object.
a connection object as produced by dbConnect.
authorization arguments needed by the DBMS instance; these typically include user, password, dbname, host, port, etc. For details see the appropriate DBIDriver.

Some implementations may allow you to have multiple connections open, so you may invoke this function repeatedly assigning its output to different objects. The authorization mechanism is left unspecified, so check the documentation of individual drivers for details.


  • An object that extends DBIConnection in a database-specific manner. For instance dbConnect("MySQL") produces an object of class MySQLConnection. This object is used to direct commands to the database engine.

    dbDisconnect returns a logical value indicating whether the operation succeeded or not.

Side Effects

A connection between R/Splus and the database server is established, and the R/Splus program becomes a client of the database engine. Typically the connections is through the TCP/IP protocol, but this will depend on vendor-specific details.


Make sure you close the connection using dbDisconnect(conn) when it is not longer needed.


See the Database Interface definition document DBI.pdf in the base directory of this package or http://developer.r-project.org/db.

See Also

dbConnect dbSendQuery dbGetQuery fetch dbCommit dbGetInfo dbReadTable

  • dbDisconnect
  • dbConnect
# create an RODBC instance and create one connection.
m <- dbDriver("RODBC")

# open the connection using user, passsword, etc., as
# specified in the file \file{\$HOME/.my.cnf}
con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))    

# Run an SQL statement by creating first a resultSet object
rs <- dbSendQuery(con, statement = paste(
                      "SELECT w.laser_id, w.wavelength, p.cut_off",
                      "FROM WL w, PURGE P", 
                      "WHERE w.laser_id = p.laser_id", 
                      "SORT BY w.laser_id")
# we now fetch records from the resultSet into a data.frame
data <- fetch(rs, n = -1)   # extract all rows
Documentation reproduced from package DBI, version 0.1-11, License: GPL (version 2 or later)

Community examples

Looks like there are no examples yet.