rr-dbiroracler-dbconnect

In R (and using ROracle), how do I successfully pull in data from my database?


In R (and using ROracle), how do I successfully pull in data from my database? It looks like everything is working, but it's not providing me my table from my database.

I have the following code:


#set path to oracle client info
Sys.setenv(OCI_LIB64="C:-------\\instantclient_19_3")

#set path to oracle product(original) info
Sys.setenv(OCI_INC="C:------11.2.0\\client_3\\oci\\include")

#self-explanatory
install.packages('ROracle')

#Add any requiredlibraries:
   install.packages("tidyverse")
   install.packages("dplyr")
   install.packages("ROracle")
---

######################   LOAD THE LIBRARIES    ############################       
#This first block loads the libraries and deletes objects from the work. Instructions for installing the ROracle library can be found at http://cran.us.r-project.org/web/packages/ROracle/INSTALL.
library(tidyverse)
library(ROracle)
library(dplyr)


######################   CLEAR THE WORKSPACE  ############################   
# Clear the workspace
rm(list=ls())

######################   SET THE WORKING DIRECTORY ############################      

setwd("G:\----\2017_Annual_Averages_R")


######################   READ DATA IN FROM ORACLE   ############################
# Set this switch to true to read the data from the DB
READ_FROM_DB = TRUE

if (READ_FROM_DB)
{
  USER_NAME <- "WATER" ###Add your username
  PASSWORD <- "AQUA" ###Add your password
  # Create the connection string
  host <- "-------.gov"
  port <- ----
  sid <- "-----"
  connect.string <- paste(
    "(DESCRIPTION=",
    "(ADDRESS=(PROTOCOL=tcp)(HOST=",
    host,
    ")(PORT=",
    port,
    "))",
   "(CONNECT_DATA=(SID=",
    sid,
    ")))",
    sep = ""
  )
}

  # Load the driver
  drv <- dbDriver("Oracle")

  # Create the connection
  con <-  dbConnect(drv, USER_NAME, PASSWORD, dbname = connect.string)


  # Read the table
 rs <- dbSendQuery(con, "SELECT * FROM
WQ_VTSSS
WHERE site_id = 'PAIN'
")

When I run all of that, it says I have data, rs, but it says it's a formal class OraResult and looks like this:

Screenshot of data

Where did I go wrong?


Solution

  • Turns out I forgot an important step to extract the rows from the resultSet (this wasn't in the instructions I received, but was in the ROracle.pdf install file)

    Here's the step I was missing:

    
      ## We now fetch records from the resultSet into a data.frame.
    data <- fetch(rs) ## extract all rows
    dim(data)