Follow all instructions given in https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX
However, there are few additional steps and hacks if isql/osql doesn't work(assuming tsql is working properly). They may sound strange, but solved the issue (isql/osql not connecting although tsql works fine) for me.
Each of the steps listed below is an outcome of debugging the osql error message.
At one or more steps, you may require additional root privileges. Grant them as necessary and revoke once the pyodbc connectivity is achieved.
The files odbc.ini
and odbcinst.ini
should also be present in /Users/<username>/etc
, apart from the official doc specified location.
There should be a copy of odbcinst.ini
in /(root directory)
as well.
The file freetds.conf
should be present in /usr/local/etc
directory.
If you are using SQL server 2014 and above, tds version should be set to 7.4. Do not set tds version to 8 since it is just an alias for 7.1.
Check whether the file /usr/local/lib/libtdsodbc.so
has execution privileges. If not, grant 766/744/777(in worst case) privileges via chmod. This step is crucial for running osql/isql and in turn pyodbc connection from python.
After these steps, re-check once the contents of odbc.ini
, odbcinst.ini
and freetds.conf
and confirm they are as per the documentation (https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX)
Run isql/osql.
If everything works fine now, you are all set up to connect to SQLServer from python. The following snippet shows the basic connection process.
import pyodbc
connection = pyodbc.connect('DSN=MYMSSQL;UID=<user_id for db_login>;PWD=<password_for db_login>)
cursor = connection.cursor()
result = cursor.execute("select @@VERSION").fetchall()
print(result)
cursor.close()
connection.close()