First argument is not an open RODBC channel

扶醉桌前 提交于 2020-02-24 18:58:12

问题


I am connecting to an Oracle database via DNS (set up the connection on the tnsnames.ora with the name "database").

I am able to succesfully run the following code:

con <- DBI::dbConnect(odbc::odbc(), 
                  "database",
                  UID = "user",
                  PWD = "password",
                  trusted_connection = TRUE)

I am also able to succesfully list all the tables in the database via:

list <- dbListTables(con)
View(list)

However, when I run:

results <- sqlQuery(channel = con, query = "select * from myschemaname.table")

I get the error:

Error in sqlQuery(channel = con, query = "select * from myschemaname.table") : 
          first argument is not an open RODBC channel

I have owner privileges and I am also able to update Excel Spreadsheets connected to this database via ODBC. If it's of any use, here's the output of str(con)

str(con)
Formal class 'Oracle' [package ".GlobalEnv"] with 4 slots
  ..@ ptr     :<externalptr> 
  ..@ quote   : chr "\""
  ..@ info    :List of 13
  .. ..$ dbname               : chr ""
  .. ..$ dbms.name            : chr "Oracle"
  .. ..$ db.version           : chr "11.02.0040"
  .. ..$ username             : chr "user"
  .. ..$ host                 : chr ""
  .. ..$ port                 : chr ""
  .. ..$ sourcename           : chr "database"
  .. ..$ servername           : chr "database"
  .. ..$ drivername           : chr "SQORA32.DLL"
  .. ..$ odbc.version         : chr "03.80.0000"
  .. ..$ driver.version       : chr "11.02.0001"
  .. ..$ odbcdriver.version   : chr "03.52"
  .. ..$ supports.transactions: logi TRUE
  .. ..- attr(*, "class")= chr [1:3] "Oracle" "driver_info" "list"
  ..@ encoding: chr ""

回答1:


You are using RODBC::sqlQuery() for a connection created with DBI::dbConnect(). Either use DBI::dbGetQuery() with DBI::dbConnect() or create a connection with RODBC::odbcConnect(), and use RODBC::sqlQuery().




回答2:


Another context, but the same error: If the table of your DB consists of numerics, you first of all have to load an object (nameofyourtable) of class xts:

    >library(RODBC)
    >library(DMwR)
    >library(xts)
    >data(nameofyourtable)



回答3:


So I had something like this connecting to a SQL Server database. In the end I had to change the Trusted_Connection=TRUE to Trusted_Connection=Yes...



来源:https://stackoverflow.com/questions/52721747/first-argument-is-not-an-open-rodbc-channel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!