Thursday 27 July 2017

From R Connect to Oracle Database


#Step 1 of 4: Install Package
#
install.packages('RJDBC')
library(RJDBC)
#Step 2 of 4 Download and point jdbc driver
#
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:\\Program Files\\R\\ojdbc6.jar")
#Step 3 of 4 Create Connection
#
con = dbConnect(jdbcDriver, 
"jdbc:oracle:thin:@//demo.us.oracle.com:1521/orcl", "scott", "tiger")
#Step 4 4of Check commands 
#
# dbReadTable: read a table into a data frame
EMP=dbReadTable(con,'EMP')
EMP
DEPT=dbReadTable(con,'DEPT')
DEPT

# dbGetQuery: read the result from a SQL statement to a data frame
table2=dbGetQuery(con,'select * from emp where deptno=10')
table2

# dbWriteTable: write a data frame to the schema. It is typically very slow with large tables.

dbSendUpdate(con,'create table dept1 as select * from dept where 1=2')
dbGetQuery(con,'select * from dept1')

dbWriteTable(con,'DEPT1',DEPT)
dbGetQuery(con,'select * from dept1')

# dbSendUpdate: execute SQL command
dbSendUpdate(con,'drop table dept1')

dbSendUpdate(con,'select * from dept') #it does not return anything