Load package
library(DBI)
library(RMySQL)
library(rJava)
library(RJDBC)
Set up the Oracle read driver
setwd(“C:\Users\Administrator\Desktop\oracle”)
Configure to connect to Oracle and read the table
drv<-JDBC("oracle.jdbc.driver.OracleDriver","ojdbc6.jar", identifier.quote=""") ##java in the JDBC routine
conn<-dbConnect(drv,"jdbc:oracle:thin: @10.113.113.177:1521:XE","SZTN","sztn") ##Establish a connection
table1<-dbGetQuery(conn,"SELECT * FROM T_SYN_YJXX_ZK01") ##Acquire data from Oracle tables according to sql records
table2< -dbGetQuery(conn,"SELECT * FROM T_SYN_SSZB_ZK01") ##Acquire the data of the table in Oracle according to the sql record
str (table1)
Create a MySQL database connection and insert data into MySQL
con <- dbConnect(MySQL(), host=“10.113.125.17”, dbname=“tan_test”, user=“root”, password=“root”)
colnames(table1) <- iconv(colnames(table1),from=“UTF-8”,to=“GBK”)
colnames(table2) <- iconv(colnames(table2),from=“UTF-8”,to=“GBK”)
dbWriteTable(con,“auto_coal_waring”,table1,overwrite=T,row.names=FALSE)
dbWriteTable(con,“auto_coal_analysis”,table2,overwrite=T,row.names=FALSE)
dbWriteTable(con,“auto_coal_waring_history”,table1,append=T,row.names=FALSE)
dbWriteTable(con,“auto_coal_analysis_history”,table2,append=T,row.names=FALSE)
Precautions
The database character encoding is different. In different system operating environments, the encoding will be different, and there will be garbled characters, which need to be set according to the system.