R-Studio - connection to Teradata is not working

耗尽温柔 提交于 2020-03-25 17:57:15

问题


I have got the issue when trying to connect my R-studio to Teradata. It gives me

Error: nanodbc/nanodbc.cpp:983: 08001: [Teradata][WSock32 DLL] (439) WSA E HostUnreach: The Teradata server can't currently be reached over this network

however, I was able to login to Teradata from cmd.

My code is using DBI connection with standard settings.

readRenviron("~/.Renviron")

databaseType <- "Teradata"
defaultDatabase <- "Test"

SERVER <- "tera2"
DATABASE <- "teradb"


driverList <- odbc::odbcListDrivers()
DRIVER <- as.character(subset(driverList, grepl(databaseType, driverList$name, ignore.case = TRUE))$name[1])
driverList <- odbc::odbcListDrivers()
DRIVER <- as.character(subset(driverList, grepl(databaseType, driverList$name, ignore.case = TRUE))$name[1])

queryData <- function(query) {

  con <- openConnection()

  queryDataRet <- data.frame(dbGetQuery(con, query))

  dbDisconnect(con)

  return(queryDataRet)

}


openConnection <- function() {

  con <- dbConnect(odbc::odbc(), 
                   Driver = DRIVER,
                   Server = SERVER,
                   DBCName = DATABASE,
                   UID = Sys.getenv("tera_user"), 
                   PWD = Sys.getenv("tera_pass"))

  #Note: passwords are stored in .Renviron

  return(con)
}


testQuery <- function(){

  query <- paste0 ("select * from test")

  print(queryData(query))

}

testQuery()

When I change anything such as password or user it gives an error so I reckon my code is correct and the connection is caused somewhere outside. Any ideas?


回答1:


For Teradata ODBC driver, DBCName= is a network name (which for other drivers would typically supplied as Server=). The keyword for the default schema is Database=.

Try something like

  con <- dbConnect(odbc::odbc(), 
               Driver = DRIVER,
               DBCName = SERVER,
               Database = defaultDatabase,
               UID = Sys.getenv("tera_user"), 
               PWD = Sys.getenv("tera_pass"))



回答2:


It seems that all codes are correct and something else plays the role as several access routes return the same error.




回答3:


Ok, after many weeks I decided to have a look at .Renviron and I used usethis::edit_r_environ() to ensure all details are correct and it worked! Thanks all for all the support.



来源:https://stackoverflow.com/questions/59413061/r-studio-connection-to-teradata-is-not-working

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