Trouble accessing oracle database using R (odbc package) - r

A colleague at work is having trouble using the odbc package function. I am trying to find help.
He is using an oracle database using R instead of running our traditional SAS programs, but he has not been successful. We are trying to find out what is causing the error messages below. Can someone help?
Attempt 1:
#Get the Oracle JDBC driver
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:/instantclient_19_10/ojdbc8.jar")
Create connection string to the Database we want
connect.string <-
glue("jdbc:oracle:thin:#//{host}:{port}/{sid}",
host = "stdbprd01.states.bls.gov",
port = 1521,
sid = "lausonep")
print(connect.string)
#Establish connection to your database
con <- dbConnect(jdbcDriver,
connect.string,
user = "username",
password = rstudioapi::askForPassword("Database password"))
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Attempt 2:
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
driver="Oracle in OraClient12Home1",
database="lausprd",
uid="aakre_n",
pwd="!QAZ1qaz#WSX2wsx",
host="stdbprd01.states.bls.gov",
port=1521
)
Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][ORA]-12560: TNS:protocol adapter error
[Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed

Related

Connect R to DB ORA -12560

I've the error ORA - 12560 when I try connect to Oracle DB installed on VM.
sql*plus, pl/sql, excel - connect successfully
Сonnection code in R
con <- dbConnect(odbc::odbc(), driver ='Oracle in instantclient_19_14',
dns = "user", encoding = "windows-1251",PWD ="qazedcwsx")
#--- output Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error
sql*plus I can connect only if after the password I write #sid, in R same method not working
Help please =(
Connected ! =)
con <- dbConnect(odbc::odbc(),
.connection_string = "Driver={Oracle in nstantclient_19_14};Dbq=XE;Uid=user;Pwd=qazedcwsx;)

ODBC Connection to ORACLE in R

I am using both the DBI and ODBC package in "R" in order to make a connection to an ORACLE database.
Here is the connection code I am using:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ORACLE",
Host = "orasada.ca",
SVC = "STG",
UID = "username",
PWD = "password",
Port = 1521)
Everything looks fine to me, but I keep getting this error:
Error: nanodbc/nanodbc.cpp:983: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any ideas on what I can change to make a successful connection?
Thanks.

RODBC connection from R to SQL failing

I'm using R Studio to connect to SQL Server Management using library(RODBC).
Note that the below code works on my local system, but does not work when trying to connect remotely.
This is my connection code:
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
I get the following warning message:
Warning messages:
1: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 08001, code 18, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error
2: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 01000, code 772, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()).
3: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
ODBC connection failed
It's really odd because I definitely have access to the server and database in question.
It's worth noting that I have access to the server from a local system (above code works) and access to a remote system (code does not work).
Any idea why this might be happening?
RODBC is not a great package, it would be much better to use the RStudio built odbc instead:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "SERVER",
Database = "DATABASE",
Trusted_Connection = "True" ,
timeout = 5000
)
In the following command line
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
make trusted_connection = FALSE or no. Its a simple case of SSL error. If you are using linux, for server, use ip address rather than hostname.

How to connect Ms SQL server with R on Ubuntu 16.04?

I am unable to connect R with MS sql using RODBC as well as RJDBC
With RODBC
library(RODBC)
dbhandle <- odbcDriverConnect(paste("driver={SQL Server};server=", sname, ";database=",
dbname, ";uid=", user,";pwd=",password, sep = ""))
res <- sqlFetch(dbhandle, client.table_name)
The error I am getting is:
Warning messages:
1: In odbcDriverConnect(paste("driver=SQL Server;server=", sname, ";database=", :
[RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
2: In odbcDriverConnect(paste("driver=SQL Server;server=", sname, ";database=", :
ODBC connection failed
Also I have tried with RJDBC
library(rJava)
library(RJDBC)
drv <- JDBC(driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver",
classPath="/home/****/sqljdbc_4.2/enu/jre8/sqljdbc42.jar")
conn <- dbConnect(drv, paste0("jdbc:sqlserver://",client.hostname),
user=client.username , password=client.password, dbname = client.dbName)
The error is
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:253fd8d2-8e60-40fb-96b0-220745818166".
I got the same error when i put below code for connection to MSSQLSERVER
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=SQL Server;Server=192.168.76.60; Database=kaggle;Uid=sa; Pwd=1234")
It throws to me
[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
why this Error thrown?
Answer: when we fail to put proper ODBC version name on Driver value.
From where we can get Driver ODBC version name
inside "/etc" folder you will find "odbcinst.ini" file open it and check the version name
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.1.so.0.1
UsageCount=1
so i got ODBC Driver name from here , it will be "ODBC Driver 17 for SQL Server"
Then i modify my connection string
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 17 for SQL Server;Server=192.168.76.60; Database=kaggle;Uid=sa; Pwd=1234")
And it works fine

RODBC odbcDriverConnect() Connection Error in shiny R but not in Rstudio

I'm trying to use
odbcDriverConnect("driver={SQL
Server};server=IP;database=DBNAME;uid=username;pwd=password")
to connecting sql server database from shiny R,
but its result is -1.
I run the same code in R studio and get results normally, what's the problem?
For testing more I tried this one:
con <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "IP",
Database = "dbname",
UID = "username",
PWD ="password",
Port = Port)
But again it runs in R studio but not in shiny and gets this error:
nanodbc/nanodbc.cpp:950: IM002: [unixODBC][Driver Manager]Data source name
not found, and no default driver specified

Resources