与create a table from a dataframe using RODBC类似的症状。
首次尝试
我已成功与readOnly=FALSE
连接到ORACLE,并可使用sqlQuery
检索数据。我创建了一个测试表 RTest ,其中包含一个名为 Value 的NUMBER(16,8)
字段。我可以使用:
sqlQuery(channel,"insert into RTest (value) values(25)")
所以我似乎有WRITE权限。
下次尝试
在几个互联网示例之后,我创建了一个R data.frame
测试,其中包含一行和一行,并在我尝试的位置命名为 Value :
sqlSave(channel, test, "RTest", fast=FALSE, append=TRUE)
和safer=TRUE
。我收到一个Oracle错误:
ORA-00922 - 缺少或无效的选项。和RODBC错误:"无法SQLExecDirect'创建表DSN = ...."
因此,sqlSave
似乎正在尝试创建表。我添加并删除了选项:rownames=FALSE
,colnames=FALSE
,safer=TRUE/FALSE
,varTypes=list
和varInfo=list
无效。
最终尝试
接下来,我删除了ORACLE中的表,并将sqlSave
与safer=TRUE
一起使用,该data.frame
应该根据data.frame
结构创建表,但会收到相同的错误。
最终,我需要定期读取R中的数据文件和进程,然后上传100多个包含数百万行的字段。针对Oracle表的查询将有助于执行分析的内存要求,并且每次都无法从压缩文件中将数据读取到R中。
有关通过RODBC将## in Oracle create table to match data types in demo data.frame USArrests
SQL> CREATE TABLE "{UserName}"."RTEST"
2 ("Murder" BINARY_DOUBLE,
3 "Assault" DECIMAL,
4 "UrbanPop" DECIMAL,
5 "Rape" BINARY_DOUBLE,
6 "State" VARCHAR2(255 BYTE)
7 );
Table created.
SQL> desc RTEST
Name Null? Type
----------------------------------------- -------- ---------------
Murder BINARY_DOUBLE
Assault NUMBER(38)
UrbanPop NUMBER(38)
Rape BINARY_DOUBLE
State VARCHAR2(255)
插入ORACLE的帮助文章在默认选项中显得很简单。我认为这是一个不费脑子的事,但这个错误不断出现。有线索吗?
新信息
取得一些进展:
1)注意到连接使用case =" nochange"表中的表和字段的ORACLE默认值是大写的。 sqlSave尝试使用小写。协调表名称情况时更改错误
2)通过反复试验发现ORACLE需要两部分名称USERNAME.TableName
3)R数字列是double,oracle字段定义为NUMERIC(16,8)[和更晚(34,17)],它应该包含必要的数字。 R生成数据类型错误试图转换二进制double。因此将Oracle切换为二进制加倍,并且该错误消失了。
对信息请求和新问题描述的响应 请参阅以下会话文本。请注意,本地服务器名称已替换为{DummyServerName},用户名替换为{UserName}。
**创建ORACLE表以匹配基本R数据帧USArrests **
## establish ORACLE connection in R
library(RODBC)
channel <- odbcConnect("{DummyServerName}", readOnly=FALSE, connection="TNS", case="nochange", believeNRows=FALSE)
odbcGetInfo(channel)
DBMS_Name DBMS_Ver Driver_ODBC_Ver Data_Source_Name Driver_Name
"Oracle" "11.02.0030" "03.52" "Default" "SQORA32.DLL"
Driver_Ver ODBC_Ver Server_Name
"11.02.0003" "03.80.0000" "{DummyServerName}"
**连接细节**
### manually insert value into table to show write capability
y <- sqlQuery(channel,"insert into {UserName}.RTEST values(25,26,27,28,'JUNK')")
y
character(0)
odbcGetInfo结果表明连接成功
**从R **
手动插入到桌面x <- sqlQuery(channel,"select * from RTest")
x
Murder Assault UrbanPop Rape State
1 25 26 27 28 JUNK
##use data frame from R examples
attach(USArrests)
The following objects are masked from USArrests (pos = 3):
Assault, Murder, Rape, UrbanPop
##capture the state names from the rownames to match ORACLE table structure
State<-rownames(USArrests)
Arrests2<-cbind(USArrests[,1:4],State)
typeof(Arrests2)
[1] "list"
class(Arrests2)
[1] "data.frame"
因此手动插入成功。我可以从R插入,但到目前为止不是从sqlSave插入。
尝试使用sqlSave
从data.frame添加值sqlSave(channel,Arrests2,tablename="{UserName}.RTEST", fast=FALSE, safer=TRUE,rownames=FALSE)
Error in sqlSave(channel, Arrests2, tablename = "{UserName}.RTEST", fast = FALSE, :
table ‘{UserName}.RTEST’ already exists
现在尝试使用sqlSave填充ORACLE表...
sqlSave(channel,Arrests2,tablename="{UserName}.RTEST", fast=FALSE, append=TRUE, rownames=FALSE)
Error in sqlSave(channel, Arrests2, tablename = "{UserName}.RTEST", fast = FALSE, :
unable to append to table ‘{UserName}.RTEST’
** OK - safer = TRUE应该附加,但看看强制附加是否可以使用append = TRUE而不是**
##In ORACLE...
SQL> drop table RTEST;
Table dropped.
**现在让R创建表**
sqlSave(channel,Arrests2,tablename="{UserName}.RTEST", fast=FALSE, safer=TRUE,rownames=FALSE)
Error in sqlSave(channel, Arrests2, tablename = "{UserName}.RTEST", fast = FALSE, :
HY000 902 [Oracle][ODBC][Ora]ORA-00902: invalid datatype
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE {UserName}.RTEST
(DSN={DummyServerName}MurderDSN={DummyServerName} binary_double,
DSN={DummyServerName}AssaultDSN={DummyServerName} decimal,
DSN={DummyServerName}UrbanPopDSN={DummyServerName} decimal,
DSN={DummyServerName}RapeDSN={DummyServerName} binary_double,
DSN={DummyServerName}StateDSN={DummyServerName} varchar(255))'
sqlSave(channel, USArrests, rownames = "state", addPK=TRUE)
Error in sqlSave(channel, USArrests, rownames = "state", addPK = TRUE) :
HY000 922 [Oracle][ODBC][Ora]ORA-00922: missing or invalid option
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE DSN={DummyServerName}USArrestsDSN={DummyServerName}
(DSN={DummyServerName}stateDSN={DummyServerName} varchar(255) NOT NULL PRIMARY KEY,
DSN={DummyServerName}MurderDSN={DummyServerName} binary_double,
DSN={DummyServerName}AssaultDSN={DummyServerName} decimal,
DSN={DummyServerName}UrbanPopDSN={DummyServerName} decimal,
DSN={DummyServerName}RapeDSN={DummyServerName} binary_double)'
**完全遵循sqlSave示例 - 这应该创建表USArrests **
sqlSave(channel, USArrests, tablename="{UserName}.USARRESTS", rownames = "state", addPK=TRUE)
Error in sqlSave(channel, USArrests, tablename = "{UserName}.USARRESTS", :
HY000 902 [Oracle][ODBC][Ora]ORA-00902: invalid datatype
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE {UserName}.USARRESTS
(DSN={DummyServerName}stateDSN={DummyServerName} varchar(255) NOT NULL PRIMARY KEY,
DSN={DummyServerName}MurderDSN={DummyServerName} binary_double,
DSN={DummyServerName}AssaultDSN={DummyServerName} decimal,
DSN={DummyServerName}UrbanPopDSN={DummyServerName} decimal,
DSN={DummyServerName}RapeDSN={DummyServerName} binary_double)'
**表名中可能需要uname限定符 - 强制表名**
sqlSave(channel, USArrests, tablename="{UserName}.USARRESTS", rownames = "state", addPK=TRUE, safer=TRUE)
Error in sqlSave(channel, USArrests, tablename = "{UserName}.USARRESTS", :
HY000 902 [Oracle][ODBC][Ora]ORA-00902: invalid datatype
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE {UserName}.USARRESTS
(DSN={DummyServerName}stateDSN={DummyServerName} varchar(255) NOT NULL PRIMARY KEY,
DSN={DummyServerName}MurderDSN={DummyServerName} binary_double,
DSN={DummyServerName}AssaultDSN={DummyServerName} decimal,
DSN={DummyServerName}UrbanPopDSN={DummyServerName} decimal,
DSN={DummyServerName}RapeDSN={DummyServerName} binary_double)'
**最后,保持uname限定符 - 强制表名,并使用应该创建表的safer = TRUE **
{{1}}
仍然困惑。最近的成功是使用现有的桌子和&#34;更安全=真实&#34;但插入失败了。检查生成错误消息的sqlSave代码显示当sqlwrite返回-1时发生错误,我认为这是一个失败。 sqlSave示例失败可能表示本地ORACLE