Search code examples
pythonsql-serverodbcpyodbc

pyodbc: unixODBC Driver error SQLAllocHandle on SQL_HANDLE_HENV failed


I am trying to query SQL Server from pyodbc using DSN with the following snippet:

import pyodbc

pyodbc.autocommit = True

conn = pyodbc.connect('DSN=SQLSERVER_CONN')
cursor = conn.cursor()
cursor.execute('select count(1) from jupiter.fact_load')
result = cursor.fetchall()

for row in result:
   print(row)

cursor.close()
conn.close()

My .odbc.ini looks:

[SQLSERVER_CONN]
Description=Connection to SQLSERVER UAT
DRIVER=/home/aiman/mssql-jdbc/9.2.0/libmsodbcsql-11.0.so.2270.0
SERVER=my.sqlserver.com,10501
DATABASE=jupiter
UID=aiman
PWD=xxxxx
Trusted_Connection=yes

And its giving me this following error:

Traceback (most recent call last):
  File "test_odbc.py", line 5, in <module>
    conn = pyodbc.connect('DSN=SQLSERVER_CONN')
pyodbc.Error: ('IM004', "[IM004] [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (0) (SQLDriverConnect)")

In one post I read it happens when .rll file is not present, but I have both the files (driver, and .rll) present at the driver's path:

libmsodbcsql-11.0.so.2270.0
msodbcsqlr11.rll

Similar question was given here (echo "default:x:$uid:0:user for openshift:/tmp:/bin/bash" >> /etc/passwd), but I can't do this since it will overwrite the system account settings, and I am trying to run from my own ID.


Solution

  • As highlighted by @AlwaysLearning, this was incompatible driver issue.
    got the compatible driver: lib64/libmsodbcsql-17.2.so.0.1.
    working fine now.