如何让R语言通过RODBC库读写数据库的数据

Python019

如何让R语言通过RODBC库读写数据库的数据,第1张

#加载RODBC包

library(RODBC)

#生成链接

channel<-odbcConnect("数据名称", "用户名", "密码")

#读取数据库中的表

data1<-sqlFetch(channel, "表名称")

#将表写入数据库,以R中自带的iris数据集为例

sqlSave(channel, iris, "表名称")

1:Obtain a package called “RODBC” in order to connect with the database.Download it here: http://cran.r-project.org/web/packages/RODBC/index.html

2:Start R, switch to the location which contains the RODBC package and an enter the following R code:

install.packages(packagename.tar.gz)

3:After the package installation you can create a channel:

ch=odbcConnect("ORACLE2")

4:Check the channel with odbcGetInfo(ch)

5:If the connection was successful, the console shows something like this:

odbcGetInfo(ch)

DBMS_Name DBMS_Ver Driver_ODBC_Ver

"Oracle" "10.02.0010" "03.52"

Data_Source_Name Driver_Name Driver_Ver

"ORACLE2" "SQORA32.DLL" "11.02.0001"

ODBC_Ver Server_Name

"03.52" "143.93.91.33:1521/xe"

1. 首先是jdbc.properties属性文件的编写,便于数据库移植:

datasource.driverClassName=oracle.jdbc.driver.OracleDriver

datasource.url=jdbc:oracle:thin:@10.6.1.11:1521:student

datasource.username=zs

datasource.password=zs

datasource.defaultAutoCommit=true