dbConnect
Create a connection to a DBMS
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 anotherdbConnect
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 appropriateDBIDriver
.
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.
Value
- An object that extends
DBIConnection
in a database-specific manner. For instancedbConnect("MySQL")
produces an object of classMySQLConnection
. 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.
References
See the Database Interface definition document
DBI.pdf
in the base directory of this package
or
See Also
dbConnect
dbSendQuery
dbGetQuery
fetch
dbCommit
dbGetInfo
dbReadTable
Examples
# 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)