RStudio连接Oracle数据库

fun <- function(){
  library(ROracle)
  drv <- dbDriver('Oracle') 
  host <- '172.24.111.100'  # 服务器IP地址
  port <- 1521              # 端口
  sid <- 'MRMSDB'          
  connect.string <- paste(
    "(DESCRIPTION=",
    "(ADDRESS=(PROTOCOL=tcp)(HOST=", host, ")(PORT=", port, "))",
    "(CONNECT_DATA=(SID=", sid, ")))", sep = "")
  con <- dbConnect(drv, username = "etl", password = "ETL", dbname = connect.string)
  #con <- dbConnect(Oracle(), "etl", "ETL")
  rs <-    dbSendQuery(con, "select * from Rpt_Option_Custom_Pre t where t.l_date = '20200701'")
  items_cntr <-  fetch(rs)
  
  #dbDisconnect(con)
  
  A    <- as.matrix(items_cntr$POS)                        #Pos
  POSITIONNAME  <- as.matrix(items_cntr$POSITIONNAME)      #POSITIONNAME
  SECURITYNAME  <- as.matrix(items_cntr$SECURITYNAME)      #SECURITYNAME
  Trade_Date     <- as.matrix(items_cntr$TRADE_DATE)       #Trade_Date
  
  B <- as.numeric(as.matrix(items_cntr$NUM))               #Num    持仓
  C <- items_cntr$TYPE                                     #Type   期权类别
  D <- as.numeric(as.matrix(items_cntr$MULTI))             #Multi  合约单位
  E <- as.numeric(as.matrix(items_cntr$K))                 #K      行权价
  F <- as.numeric(as.matrix(items_cntr$F))                 #F  
  G <- as.numeric(as.matrix(items_cntr$V))                 #V
  H <- as.numeric(as.matrix(items_cntr$SKEW))              #Skew
  I <- as.numeric(as.matrix(items_cntr$KTC))               #Ktc
  J <- as.numeric(as.matrix(items_cntr$KTP))               #Ktp
  K <- as.numeric(as.matrix(items_cntr$TE))                #tE
  L <- as.numeric(as.matrix(items_cntr$TD))                #tD
  X <- as.numeric(as.matrix(items_cntr$X))                 #标的收盘价
  XA <- as.numeric(as.matrix(items_cntr$R))                #R
  XB <- as.numeric(as.matrix(items_cntr$UP_AND_DOWN))      #涨跌幅
  XC <- as.numeric(as.matrix(items_cntr$VOLATILITY))       #波动率
    
  #ifelse(test, yes, no)
  KL <- as.numeric(as.matrix(ifelse(C=="C",I,J))) 
  SO <- as.numeric(as.matrix(log(F/E)/sqrt(K)))            #期权ZZ                                                    

  resultOfAll <- data.frame(option_ZZ=SO,POSITIONNAME=POSITIONNAME,SECURITYNAME=SECURITYNAME,Trade_Date=Trade_Date)
  resultOfAll    
}
OracleRscript

我来吐槽

*

*