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

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

Related

Trying to work on R using Azure ML Studio Notebook and facing challenges with ODBC package

I am trying to work on R notebook on ML Studio. Using regular python is easy and works as expected but with R i am facing challenges.
While trying to connect to MS SQL database using odbc() :
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server",
Database = "db",
UID = "user",
PWD = "password",
Port = 1433)
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
As suggested in some posts, i have also tried replacing Driver = "SQL Server", with Driver = "ODBC Driver 11 for SQL Server". But i see similar error
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 11 for SQL Server' : file not found
Traceback:
Please suggest a work around.
We can assign the Driver as the version of the SQL server and get the password using the API call.
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 13 for SQL Server",
Server = "your server IP address",
Database = "Database name",
UID = "User ID",
PWD = rstudioapi::askForPassword("password"),
Port = port_number_under_user)
In some cases, Driver = "SQL Server” will also work fine. If not functioning. Use the above code block.

How can I connect to an Azure SQL Server Database from R?

I am trying to connect to an Azure SQL Server Database from R, so I can create a table and upload data to the database.
This is how I connect to the server in SQL Server Management Studio
I have tried the following:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server_name, 1433",
Database = "database_name",
uid = "me#domain.com",
pwd = "password")
This results in the error:
Error: nanodbc/nanodbc.cpp:1021: HY000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server "domain.com" requested by the login. The login failed.
library(RODBC)
con.text <- paste("DRIVER={"SQL Server"}",
";Database=database_name",
";Server=server_name",
",1433",
";PROTOCOL=TCPIP",
";trusted_connection=true",
sep="")
con <- odbcDriverConnect(con.text)
This results in the warning messages:
1: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state HY000, code 40607, message [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server.
2: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
3: In odbcDriverConnect(con.text) : ODBC connection failed
How can I successfully connect to this server from R?
Error: nanodbc/nanodbc.cpp:1021: HY000: [Microsoft][ODBC SQL Server
Driver][SQL Server]Cannot open server "domain.com" requested by the
login. The login failed.
The above error is mainly caused because of driver problem. Use this command to check whether drivers are properly installed or not.
library(odbc)
sort(unique(odbcListDrivers()[[1]]))
Connect Azure SQL database from R ,follow this Syntax:
library(odbc)
library(DBI)
library(RODBC)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "[your driver's name]",
Server = "[your server's path]",
Database = "[your database's name]",
UID = "Database user",
PWD = "Database password",
Port = 1433)
Please follow the below reference it has detailed information about connecting Azure SQL to R in a detailed way:
Connecting to Azure SQL via R
https://db.rstudio.com/getting-started/connect-to-database
https://www.youtube.com/watch?v=ZkcBwYjHYOc

Trouble accessing oracle database using R (odbc package)

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

Connect to remote SQL Server with R

I'm trying to connect R to a remote SQL Server based on this. I use
library(DBI)
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "td",
host = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
username = "trades",
password = "u#6MS06")
but the connection is not working.Failed to connect to database: Error: Can't connect to MySQL server on 'hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com' (0) What do I miss? Note that I do not give the real credentials here. Then I want to connect with a table named "rep_user_listings".
After installing `ODBC Driver 11 for SQL Server I have also used :
library(DBI)
conn <- dbConnect(
drv = odbc::odbc(),
driver = "ODBC Driver 11 for SQL Server",
database = "td",
server = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
uid = "trade",
pwd = "u#6MS06X")
and I get:
Error: nanodbc/nanodbc.cpp:950: 08001: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
and
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=trade; Pwd=u#6MS06Xv; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from rep_user_listings;"))
odbcClose(dbconnection)
and I get:
Warning messages:
1: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state 08001, code -1, message [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
2: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state HYT00, code 0, message [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
3: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state 08001, code -1, message [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
4: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
ODBC connection failed
There isn't a native SQL Server client in development for R, the most recent I'm aware of (https://github.com/imanuelcostigan/RSQLServer) is archived/deprecated in lieu of the odbc package.
Try this:
library(DBI)
conn <- dbConnect(
drv = odbc::odbc(),
driver = "ODBC Driver 17 for SQL Server",
database = "td",
server = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
uid = "trades",
pwd = "u#6MS06")
(And if that works, immediately change your password. :-)
I chose "ODBC Driver 17 for SQL Server" because I previously installed the mssql driver for my windows and linux machines (current version available here), and found the driver "name" here:
unique(odbc::odbcListDrivers()$name)
# [1] "SQL Server" "PostgreSQL ANSI(x64)"
# [3] "PostgreSQL Unicode(x64)" "SQLite3 ODBC Driver"
# [5] "ODBC Driver 17 for SQL Server"
If you have an older version installed (such as 11 or 13) or something else, use that driver instead (or upgrade).
Firstly, don't give out your credentials online. Based on what i am looking at, open your terminal and ping your server to make sure you have network access to your server.
If you are using Linux one of the known issues is dont use domain name use ipaddess if you have also, make trusted connection =no.

Conntecting to SQL Server by R on macOS

I have a problem to connect to my external sql server database on R. I am working on macOS. I tried to do everything as in tutorial: LINK
My code:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(), Driver = "SQL Server", Server = "server", Database = "DataBaseName", UID = "Login", PWD = "password", Port = 1433)
but I still get this same error:
ERROR: nanodbc/nanodbc.cpp:950: 01000: [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
Does anyone know how to solve this problem? I will be very grateful!

Resources