DBI (version 0.1-11)

dbConnect: Create a connection to a DBMS

Description

Connect to a DBMS going through the appropriate authorization procedure.

Usage

dbConnect(drv, ...)
  dbDisconnect(conn, ...)

Arguments

drv
an object that inherits from DBIDriver, a character string specifying the DBMS driver, e.g., "RPgSQL", "ROracle", "Informix", or possibly another dbConnect object.
conn
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.

Value

  • 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.

notes

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

Details

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.

References

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

Examples

Run this code
# 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
dim(data)

Run the code above in your browser using DataCamp Workspace