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

元气小坏坏 提交于 2019-11-28 03:24:04

问题


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?


回答1:


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




回答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')



来源:https://stackoverflow.com/questions/43606832/r-unixodbcdriver-managercant-open-lib-sql-server-file-not-found

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