2

我安装了 ROracle(按照包中的说明)并成功连接到我们的 Oracle 数据库。

我可以运行查询,使用dbGetQuery,并得到很好的结果,例如:

> dbGetQuery(con, "select count(*) from table_name")
  COUNT(*)
1     6111

但是,其他一些 DBI/ROracle 辅助函数没有给出任何结果:

> dbListTables(con)
character(0)

> dbReadTable(con, "table_name")
Error in .oci.GetQuery(con, qry) : 
  ORA-00942: table or view does not exist

任何想法可能是什么原因?

4

2 回答 2

1

在这两种情况下,如果我指定一个schema参数,它们都对我有用,即

dbListTables(con, schema = "my_schema")
dbReadTable(con,"table_name",schema = "my_schema")

此外,从阅读?dbListTables中可以看出,它具有控制是否查看所有模式all以及full是否返回完整模式名称或仅返回表名的参数。

于 2016-09-07T16:19:05.923 回答
0

After spending too much time trying to get to the bottom of this issue, I want to record the answer for posterity. Actually having the schema is not necessary in dbReadTable, but putting the table name in uppercase is. That is, the call should be

dbReadQuery(con, "TABLE_NAME")

Why?

To find out how dbReadTable is different from a dbGetQuery with a select * from table call, I dug for the source:

> showMethods("dbReadTable")
Function: dbReadTable (package DBI)
conn="OraConnection", name="character"

So it's an S4 method. We use getMethod with the above signature:

> getMethod("dbReadTable", signature = c(conn = "OraConnection", name = "character"))
Method Definition:

function (conn, name, ...) 
{
    .local <- function (conn, name, schema = NULL, row.names = NULL, 
        ...) 
    .oci.ReadTable(conn, name, schema = schema, row.names = row.names)
    .local(conn, name, ...)
}
<environment: namespace:ROracle>

.oci.ReadTable can be found here, in the source of ROracle. All it does is check argument validity, call dbGetQuery, and set row names. The relevant part (calling dbGetQuery) is here:

# form name
if (is.null(schema))
  tab <- sprintf('"%s"', name)
else
  tab <- sprintf('"%s"."%s"', schema, name)

# read table
qry <- paste('select *',
               'from', tab)
res <- .oci.GetQuery(con, qry)

Note that if a schema isn't specified, the table name is used without the "schema." prefix. However, sprintf produces a quoted string for the table name, and quoted table names are case sensitive! (dbGetQuery just passes an unquoted table name which can be lowercase.)

So that's why dbGetQuery(con, "select count(*) from table_name") works, as does dbReadQuery(con, "TABLE_NAME") but dbReadQuery(con, "table_name") doesn't.

于 2016-09-07T21:41:36.740 回答