3.2.3.2 データベース表の作成および削除

ore.create関数を使用して、Oracle Databaseスキーマの永続表を作成します。

ノート:

Oracle Machine Learning for Rで表を作成するときに表の名前に小文字または大/小文字の組合せを使用する場合、SQL問合せまたは関数で表を使用するときは、同じ小文字または大/小文字の組合せの名前を二重引用符で囲って使用する必要があります。表を作成するときにすべて大文字の名前を使用する場合、表の名前は大/小文字が区別されません。二重引用符を使用せずに表を使用する場合、大文字、小文字、または大文字/小文字の組合せを使用できます。これは、表の列に名前を付ける場合も同様です。

表を作成すると、データベース・スキーマを表すR環境に表のore.frameプロキシ・オブジェクトが自動的に作成されます。プロキシore.frameオブジェクトには表と同じ名前が付けられます。ore.drop関数を使用して、Oracle Databaseスキーマの永続表を削除できます。

注意:

ore.drop関数は、データベース表およびそれに関連付けられたore.frameプロキシ・オブジェクトを削除する場合にのみ使用します。永続データベース表と関連付けられてないore.frameオブジェクトを削除する場合は、これを使用しないでください。一時データベース表のore.frameオブジェクトを削除する場合は、ore.rm関数を使用します。

例3-13 表を作成および削除するためのore.createおよびore.dropの使用方法

この例では、データベース内に表を作成し、それらの一部を削除します。

# Create the AIRQUALITY table from the data.frame for the airquality data set.
ore.create(airquality, table = "AIRQUALITY")
# Create data.frame objects.
df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
# Create the DF1 and DF2 tables from the data.frame objects.
ore.create(df1, "DF1")
ore.create(df2, "DF2")
# Create the CARS93 table from the data.frame for the Cars93 data set.
ore.create(Cars93, table = "CARS93")
# List the OML4R proxy objects.
ore.ls()
# Drop the CARS93 object.
ore.drop(table = "CARS93")
# List the OML4R proxy objects again.
ore.ls()

この例のリスト

R> # Create the AIRQUALITY table from the data.frame for the airquality data set.
R> ore.create(airquality, table = "AIRQUALITY")
R> # Create data.frame objects.
R> df1 <- data.frame(x1 = 1:5, y1 = letters[1:5])
R> df2 <- data.frame(x2 = 5:1, y2 = letters[11:15])
R> # Create the DF1_TABLE and DF2_TABLE tables from the data.frame objects.
R> ore.create(df1, "DF1")
R> ore.create(df2, "DF2")
R> # Create the CARS93 table from the data.frame for the Cars93 data set.
R> ore.create(Cars93, table = "CARS93")
R> # List the OML4R proxy objects.
R> ore.ls()
[1] "AIRQUALITY"  "CARS93"  "DF1"  "DF2_"
R> # Drop the CARS93 object.
R> ore.drop(table = "CARS93")
R> # List the OML4R proxy objects again.
R> ore.ls()
[1] "AIRQUALITY"  "DF1_"  "DF2"

ノート:

4000文字を超えるテキスト問合せ、または4000文字を超える値をCLOB列に格納すると、「ORA-01704: 文字列リテラルが長すぎます」というエラーが発生します。次に示すようにデータが大きい場合は、バインド変数を使用します。バインド変数の詳細は、ROracleを参照してください。

library(ROracle)
options(error = expression(NULL))
Sys.setlocale(‘LC_ALL’, ‘C’)
cat(‘\n Welcome to ROracle(OCI) World\n’);
cat(‘\n DBI Version : ’);
print(packageVersion(‘DBI’));
cat(‘\n’);
#Creating table whose fields are of different type
createStr <- ‘create table TMRQORABND1_TAB(row_num number, id1 clob)’;
insStr <- ‘insert into TMRQORABND1_TAB values(:1, :2)’;
selStr <- ‘select * from TMRQORABND1_TAB order by row_num’;
y <- ‘1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef’;
z <- y
z <- paste(y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, ‘1234567890abcdef1234567890abcdef’, sep = ‘’);
c32767 <- paste(z, z, z, z, z, z, z, z, y, y, y, y, y, y, y, y, y, y, y,
          ‘1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcde’,
          sep = ‘’)
print(nchar(c32767))
c32766 <- paste(z, z, z, z, z, z, z, z, y, y, y, y, y, y, y, y, y, y, y,
          ‘1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd’,
          sep = ‘’)
print(nchar(c32766))
c32768 <- paste(z, z, z, z, z, z, z, z, y, y, y, y, y, y, y, y, y, y, y,
          ‘1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef’,
          sep = ‘’)
print(nchar(c32768))
y <- paste(y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, ‘1234567890abcdef1234567890abcdef’, sep = ‘’);
y1 <- y
y <- paste(y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, ‘1234567890abcdef1234567890abcdef’, sep = ‘’);
y2 <- y
y <- paste(y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
          y, y, ‘1234567890abcdef1234567890abcdef’, sep = ‘’);
y3 <- y
y4 <- paste(y3, y3, y3, y3, y3)
r1c2 <- paste(y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
             y, y, ‘1234567890abcdef1234567890abcdef’, sep = ‘’);
print(nchar(y));
drv <- dbDriver(‘Oracle’);
cat(‘\n ROracle driver allocated.\n’);
con <- dbConnect(drv,‘scott’,‘tiger’);
cat(‘\n One database connection object created.\n’);
#tryCatch(
#{
 if (dbExistsTable(con, ‘TMRQORABND1_TAB’))
   dbGetQuery(con,‘drop table TMRQORABND1_TAB’);
 dbGetQuery(con, createStr);
 cat(‘\nTable created with columns data type as raw(n) \n’);
 x <- 1;
 dbGetQuery(con, insStr, data.frame(x,r1c2));
 dbCommit(con);
 x <- c(2, 3, 4, 5, 6, 7, 8, 9, 10);
 yy <- c(y1, y2, y3, z, y4, c32767, c32766, c32768, ‘’);
 dbGetQuery(con, insStr, data.frame(x, yy));
 dbCommit(con)
 print(dbGetQuery(con, ‘select row_num, length(id1) from TMRQORABND1_TAB’));
 x <- 100;
 y <- paste(y, c32767, sep = ‘’);
 dbGetQuery(con, insStr, data.frame(x,y));
 dbCommit(con)
 s <- dbSendQuery(con, selStr)
 cinfo <- dbColumnInfo(s)
 print(dbGetQuery(con, ‘select row_num, length(id1) from TMRQORABND1_TAB’));
 res <- dbGetQuery(con, selStr)
 if (res[,2][1] != r1c2) {
   print(paste(‘Row’, res[,1][1], cinfo[,1][2], res[,2][1],
               ‘not equal to’, r1c2))
 } else {
   print(paste(‘Row’, res[,1][1], cinfo[,1][2], ‘length is’,
               nchar(res[,2][1]),
               ‘length of data is’, nchar(r1c2)), sep = ‘’)
 }
 for (i in 2:9)
 {
   if (res[,2][i] != yy[i-1]) {
     print(paste(‘Row’, res[,1][i], cinfo[,1][2], res[,2][i],
                 ‘not equal to’, yy[i-1]))
   } else {
     print(paste(‘Row’, res[,1][i], cinfo[,1][2], ‘length is’,
                 nchar(res[,2][i]),
                 ‘length of data is’, nchar(yy[i-1])), sep = ‘’)
   }
 }
 if (!is.na(res[,2][10])) {
   print(paste(‘Row’, res[,1][10], cinfo[,1][2], res[,2][10],
               ‘not equal to’, yy[9]))
 } else {
   print(paste(‘Row’, res[,1][10], cinfo[,1][2], ‘length is’,
               nchar(res[,2][10]),
               ‘length of data is’, nchar(yy[9])), sep = ‘’)
 }
 if (res[,2][11] != y) {
   print(paste(‘Row’, res[,1][11], cinfo[,1][2], res[,2][11],
               ‘not equal to’, y))
 } else {
   print(paste(‘Row’, res[,1][11], cinfo[,1][2], ‘length is’,
               nchar(res[,2][11]),
               ‘length of data is’, nchar(y)), sep = ‘’)
 }
#}, finally = {
 dbGetQuery(con,‘drop table TMRQORABND1_TAB’);
 cat(‘\n ROracle driver deallocated successfully.\n’);
 cat(‘Releasing resources...‘);
 dbDisconnect(con);
 cat(‘\n Connection with database removed successfully.\n’);
 dbUnloadDriver(drv);
 cat(‘done\n’);
#}) # tryCatch()