简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM