21
setwd("/mnt/mountpoint/abc/")
sqlServerConnString <- "SERVER=server;DATABASE=sqldwdb;UID=xyz;PWD=abc;"
sqlServerDataDS <- RxSqlServerData(sqlQuery = "SELECT * FROM xyz",
                               connectionString = sqlServerConnString)
sqlServerDataDF <- rxImport(sqlServerDataDS)

This is my code. I am getting the followin error in R

[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found

[unixODBC][Driver Manager]Connnection does not exist ODBC Error in SQLDisconnect Could not open data source.Error in doTryCatch(return(expr), name, parentenv, handler) : Could not open data source.

I have installed MSSQL and unixODBC driver on my linux machine and it is getting refelected in /etc/odbc.ini file too

Can someone please help me in this?

nrussell
  • 18,382
  • 4
  • 47
  • 60
Sonal
  • 561
  • 2
  • 6
  • 15
  • You seem to be missing `DRIVER=SQL Server;` from the beginning of your connection string. – nrussell Apr 25 '17 at 10:35
  • I have added it now. Still facing the same issue – Sonal Apr 25 '17 at 10:56
  • 2
    This error is passed through Microsoft R Server directly from the ODBC driver, have you successful made an ODBC connection from outside of Microsoft R Server to your SQL Server? Could you please include the version information for Microsoft R Server? – Kirill Glushko - Microsoft Apr 26 '17 at 00:39
  • 2
    @KirillGlushko-Microsoft: Thanks for your help. I checked my SQL connection through Linux Terminal. It was giving error before because the location of the file was wrong in `/etc/odbcinst.ini` file. I corrected that and it worked for me. Thanks a lot. – Sonal Apr 26 '17 at 10:51
  • @Sonal: How and what did you corrected about the file location in `/etc/odbcinst.ini`? – Slimshadddyyy Apr 04 '18 at 06:50
  • I get the same problem in mac, while trying to run on Rstudio with odbc library. Any clues how to solve this? – tavalendo Jun 15 '18 at 08:09
  • The library(odbc) [readme](https://github.com/r-dbi/odbc/blob/master/README.md#connecting-to-a-database) provides a detailed guide concerning the necessary steps to connect to MSSQL. – ismirsehregal Nov 28 '18 at 09:00

3 Answers3

36

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

Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
2

In my case Zaynul answer didn't worked, unfortunately. But it helped me to find another way. I am using sqlalchemy and I could sucessfully create a connection passing the driver path:

engine = create_engine('mssql+pyodbc://sa:******@localhost:1433/dbCVServer?driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1', echo=True)

engine.execute('select 1')

EDIT: I was not satisfied with that solution, since the file name of driver could change, so I managed to change the library used to connect do pymssql.

engine = create_engine('mssql+pymssql://sa:******@localhost:1433/dbCVServer')

and it worked well too. =]

EDIT 2: Another discovery, connecting to a named instance, for those using sql express, for instance.

engine = create_engine('mssql+pymssql://mssql+pymssql://localhost\SQLEXPRESS/dbCVServer')

  • Instead of putting installation path to libmsodbcsql library you can reference it in the sqlalchemy URL like `engine = create_engine('mssql+pyodbc://sa:******@localhost:1433/dbCVServer?driver=ODBC+Driver+17+for+SQL+Server` – Tom Close Dec 03 '19 at 00:16
-2

This works for me:

con <- DBI::dbConnect(
  odbc::odbc(),
  Driver   = "SQL Server",
  Server   = "server",
  Database = "database",
  UID      = "uid",
  PWD      = "pwd"
)
Dmitry Ishutin
  • 396
  • 2
  • 13