ROracle各种命令用法


dbCommit-methods

说明:在Oracle里事务的提交和回滚

 

使用方法:

dbCommit(conn, ...)

dbRollback(conn, ...)

 

举例:

drv <-dbDriver("Oracle")

con <- dbConnect(drv,"scott", "tiger")

dbReadTable(con,"EMP")

rs <- dbSendQuery(con,"delete from emp where deptno = 10")

dbReadTable(con,"EMP")

if(dbGetInfo(rs, what ="rowsAffected") > 1)

{

warning("dubious deletion-- rolling back transaction")

dbRollback(con)

}

dbReadTable(con,"EMP")

 

 

dbConnect-methods

说明:创建一个数据库连接方法

 

使用方法:

## S4 method for signature 'OraDriver'

dbConnect(drv, username = "",password = "", dbname = "", prefetch = FALSE,

bulk_read = 1000L, stmt_cache = 0L,external_credentials = FALSE,sysdba = FALSE, ...)

## S4 method for signature 'ExtDriver'

dbConnect(drv, prefetch = FALSE, bulk_read= 1000L, stmt_cache = 0L,

external_credentials = FALSE, sysdba =FALSE, ...)

## S4 method for signature 'OraConnection')

dbDisconnect(conn, ...

 

参数说明:

drv: OraDriver 或 ExtDriver 驱动

conn: 连接oracle对象

username: 数据库用户名

password: 数据库用户密码

dbname: 连接oracle数据库 tns的别名

prefetch: 一个逻辑值,TRUE或FALSE。当设置为TRUE,ROracle会 使用OCI预取缓冲器,以从服务器中检索附加数据,从而节省通过从OCI提取数据分配一个行缓冲区获取的RODBI/ ROOCI所需的内存。在使用预取结果取的每一行。通过默认情况下,预取为FALSE且数组提取用于检索从所述数据服务器。

bulk_read: 一个整数值,表示每次提取的行数。默认 值1000L。当选择了预取的选项,内存分配用于 预取缓冲器和每次每次会取的行数。当预取 不使用(默认值),内存是分配在RODBI/ ROOCI定义缓冲区。将其设置为一个较大的值会导致分配更多的内存基础上,在选择列表中的列和列类型的数量。对于字符类型的列,定义使用最大宽度乘以缓冲区分配 NLS的最大宽度。应用程序应该调整基础上,该值查询结果和更大的值将有利于返回大结果的查询。一个应用程序可以调整为所需要的值。

stmt_cache: 一个整数值,表示语句高速缓存的个数。这意味着游标不需要再次解析就能准备使。默认值是0L。如果stmt_cache值大于0L,则预取的值必须设置为TRUE。

external_credentials: 一个逻辑值,TRUE或FALSE。当设置为TRUE,ROracle会开始OCI会话连接外部凭证进行身份验证。 缺省值是FALSE。

sysdba: 一个逻辑值,TRUE或FALSE。当设置为TRUE,ROracle会 首先在连接上SYSDBA权限OCI会话。默认 值为FALSE。

 

举例:

## 在本机创建一个数据库实例和连接

drv <-dbDriver("Oracle")

con <- dbConnect(drv,username = "scott", password = "tiger")

 

## 运行一个SQL语句

rs <- dbSendQuery(con,"select * from emp where deptno = 10")

 

## 读取一个结果集到数据框

data <- fetch(rs) ## extractall rows

dim(data)

 

 

## 在连接远程数据库的方法:

## create an Oracle Databaseinstance and create one connection to a

## remote database using theSID in the connect string.

drv <-dbDriver("Oracle")

 

## refer to Oracle Database NetServices Administator's Guide for

## details on connect stringspecification.

host <- "myhost"

port <- 1521

sid <- "mysid"

connect.string <- paste(

"(DESCRIPTION=",

"(ADDRESS=(PROTOCOL=tcp)(HOST=",host, ")(PORT=", port, "))",

"(CONNECT_DATA=(SID=",sid, ")))", sep = "")

 

## use username/passwordauthentication

con <- dbConnect(drv,username = "scott", password = "tiger",

dbname = connect.string)

 

## run a SQL statement bycreating first a resultSet object

rs <- dbSendQuery(con,"select * from emp where deptno = 10")

 

## we now fetch records fromthe resultSet into a data.frame

data <- fetch(rs) ## extractall rows

dim(data)

 

 

dbDriver-methods

说明:Oracle驱动的初始化和关闭

 

使用方法:

## S4 method for signature 'OraDriver'

dbUnloadDriver(drv, ...)

## S4 method for signature 'ExtDriver'

dbUnloadDriver(drv, ...)

 

举例:

# create an Oracle instance

drv <-dbDriver("Oracle")

con <- dbConnect(drv,"scott", "tiger")

res <- dbSendQuery(con,"select * from emp")

fetch(res, n = 5)

fetch(res)

dbClearResult(res)

dbUnloadDriver(drv)

 

 

dbGetInfo-methods

说明:获取对象的信息的函数

 

举例:

drv <-dbDriver("Oracle")

con <- dbConnect(drv,"scott", "tiger")

rs <- dbSendQuery(con,"select * from emp")

dbGetStatement(rs)

dbHasCompleted(rs)

dbGetInfo(rs)

 

# DBIDriver info

names(dbGetInfo(drv))

 

# DBIConnection info

names(dbGetInfo(con))

 

# DBIResult info

names(dbGetInfo(rs))

 

 

dbListConnections-methods

说明:获取连接对象的信息的函数

 

使用方法:

## S4 method for signature 'OraDriver'

dbListConnections(drv, ...)

## S4 method for signature 'ExtDriver'

dbListConnections(drv, ...)

## S4 method for signature 'OraConnection'

dbListResults(conn, ...)

 

举例:

drv <-dbDriver("Oracle")

con1 <- dbConnect(drv,"scott", "tiger")

res1 <- dbSendQuery(con1,"select * from emp where deptno = 10")

res2 <- dbSendQuery(con1,"select * from emp where deptno = 20")

con2 <- dbConnect(drv,"scott", "tiger")

res3 <- dbSendQuery(con2,"select * from dept")

 

## get all active statements

for(con indbListConnections(drv))

for (res in dbListResults(con))

print(dbGetStatement(res))

 

dbReadTable-methods

说明:在数据库中执行get, assign,exists, remove, objects, and names except的操作

 

使用方法:

## S4 method for signature'OraConnection,character'

dbReadTable(conn, name, schema = NULL,row.names = NULL, ...)

## S4 method for signature'OraConnection,character,data.frame'

dbWriteTable(conn, name, value, row.names =FALSE, overwrite = FALSE,

append = FALSE, ora.number = TRUE, schema =NULL, ...)

## S4 method for signature'OraConnection,character'

dbExistsTable(conn, name, schema = NULL,...)

## S4 method for signature 'OraConnection,character'

dbRemoveTable(conn, name, purge = FALSE,schema = NULL, ...)

## S4 method for signature 'OraConnection'

dbListTables(conn, schema = NULL, all =FALSE, full = FALSE, ...)

## S4 method for signature'OraConnection,character'

dbListFields(conn, name, schema = NULL,...)

 

参数说明:

conn: 连接数据库的对象

name: 数据库表名(大小写敏感)

schema: 数据库用户(大小写敏感)

row.names: 在dbReadTable情况下,这个变量可以是字符串,索引或逻辑矢量指定列中的DBMS表被用作在row.names 输出data.frame(一个NULL指定的列不应该被用来作为row.names 在输出中)。默认值为NULL。

          在dbWriteTable的情况下,这个参数应该是一个逻辑值,表明是否row.names应该输出到输出DBMS表;如果为true,一个名称是“row.names”额外的列其将被添加到输出。默认为FALSE。

 

value: 一个包含数据的写到表里的data.frame

overwrite: 一个逻辑值,是否覆盖表中的数据,默认为FALSE

append: 一个逻辑值,是否是追加数据到已存在的表,默认为FALSE

ora.number: 一个逻辑值,指明是否要创建一个表与OracleNUMBER或 BINARY_DOUBLE列当在写数字数据时。默认值为TRUE。

purge: 逻辑值,指明是否将purge:选项添加到SQL DROPTABLE语句中。

all: 逻辑值,是否查看所有schemas

full: 逻辑值,指明是否生成schema名。当参数都是TRUE时,输出是一个包含模式名向量,其次是表名。在输出用矩阵(...,NCOL= 2)产生一个矩阵其中每一行对应于一个表中,列表示的模式名和表名

 

ROracle methods such asdbReadTable, dbGetQuery, fetch, and dbWriteTable use the following mapping between R andOracle data types:

●  logical and integer map to Oracle INTEGER

●  numeric maps to Oracle NUMBER if argument ora.number is TRUE orOracle BINARY_DOUBLEif FALSE

●  character maps to Oracle VARCHAR2(4000)

●   Date and POSIXct map to Oracle DATE ROracle - the ROracle package R- the R application POSIXct - the POSIXct class TIMESTAMP TIMESTAMP WITH TIMEZONE TIMESTAMP WITH LOCAL TIME ZONE

●  difftime maps to Oracle INTERVAL DAY TO SECOND

●  list of raw vectors map to Oracle RAW(2000)

●   other R types such as factor are converted to character



举例:

con <- dbConnect(Oracle(),"scott", "tiger")

if (dbExistsTable(con,"FOO", "SCOTT"))

dbRemoveTable(con,"FOO")

foo <- dbReadTable(con,"EMP")

row.names(foo) <- foo$EMPNO

 

foo <- foo[,-1]

dbWriteTable(con,"FOO", foo, row.names = TRUE)

dbWriteTable(con,"FOO", foo, row.names = TRUE, overwrite = TRUE)

dbReadTable(con,"FOO", row.names = 1)

dbGetQuery(con, "deletefrom foo")

dbWriteTable(con,"FOO", foo, row.names = TRUE, append = TRUE)

dbReadTable(con,"FOO", row.names = 1)

dbRemoveTable(con,"FOO")

dbListTables(con)

dbListFields(con,"EMP")

if (dbExistsTable(con,"RORACLE_TEST", "SCOTT"))

dbRemoveTable(con,"RORACLE_TEST")

 

# example of POSIXct usage

# A table is created using:

createTab <- "createtable RORACLE_TEST(row_num number, id1 date,

id2 timestamp, id3 timestampwith time zone,

id4 timestamp with local timezone )"

dbGetQuery(con, createTab)

 

# Insert statement

insStr <- "insert intoRORACLE_TEST values(:1, :2, :3, :4, :5)";

 

# Select statement

selStr <- "select *from RORACLE_TEST";

 

# Insert time stamp withouttime values in POSIXct form

x <- 1;

y <- "2012-06-05";

y <- as.POSIXct(y);

dbGetQuery(con, insStr,data.frame(x, y, y, y, y));

 

# Insert date & times stampwith time values in POSIXct form

x <- 2;

y <- "2012-01-0507:15:02";

y <- as.POSIXct(y);

z <- "2012-01-0507:15:03.123";

z <- as.POSIXct(z);

dbGetQuery(con, insStr,data.frame(x, y, z, z, z));

 

 

# Insert list of date objectsin POSIXct form

x <- c(3, 4, 5, 6);

y <- c('2012-01-05','2011-01-05', '2013-01-05', '2020-01-05');

y <- as.POSIXct(y);

dbGetQuery(con, insStr,data.frame(x, y, y, y, y));

dbCommit (con)

 

 

# Selecting data and displayingit

res <- dbGetQuery(con,selStr)

res[,1]

res[,2]

res[,3]

res[,4]

res[,5]

 

dbSendQuery-methods

说明:查询返回数据库的结果集

 

举例:

drv <-dbDriver("Oracle")

con <- dbConnect(drv,"scott", "tiger")

res <- dbSendQuery(con,"select * from emp where deptno = :1",

data = data.frame(deptno = 10))

data <- fetch(res, n = -1)

res2 <- dbSendQuery(con,"select * from emp where deptno = :1",

data1 = data.frame(deptno =10), prefetch=TRUE,

bulk_read=2L)

data1 <- fetch(res2, n = -1)

res3 <- dbSendQuery(con,"select * from emp where deptno = :1",

data2 = data.frame(deptno =10), bulk_read=10L)

data2 <- fetch(res3, n = -1)

res4 <- dbSendQuery(con,"select * from emp where ename = :1",

data3 = data.frame(ename ='SMITH'))

data3 <- fetch(res4, n = -1)

 

 

 

你可能感兴趣的:(ROracle各种命令用法)