Rob Weyant bio photo

Rob Weyant

Data Scientist at Powerley

Twitter LinkedIn Instagram Github Last.fm

There are many ways to connect to a database in R, the one I prefer is to use the RJDBC package, because it’s fairly straight forward and simple to use RJDBC. You need to get your driver, and then define the DSN. Then you create a connection object and are good to go.

library(RJDBC)
drv <- JDBC('oracle.jdbc.OracleDriver','/path/to/driver/ojdbc6.jar')
connectionString <- "jdbc:oracle:thin:@//YOUR_DSN/"
connection <- dbConnect(drv,connectionString,YOUR_USER_NAME,YOUR_PWD)

Now you can use all sorts of functions to send requests to the database.

dbGetQuery(connection, 'select * from table')

When you’re done, you can and should disconnect with dbDisconnect function.

dbDisconnect(connection)

Connecting to Teradata Database

This process is almost identical. First, you need to change the driver. Then the connectionString format is slightly different.

drv <- JDBC('com.teradata.jdbc.TeraDriver')
connectionString <- "jdbc:teradata::@//YOUR_DSN/database=YOUR_DATABASE"
connection <- dbConnect(drv,connectionString,YOUR_USER_NAME,YOUR_PWD)

Now you’re ready to send requests to a Teradata machine.

More Resources