I am trying to connect to an Azure SQL Server database, but I keep getting this error:
(pyodbc.InterfaceError) ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I'm using this code snippet to try connecting:
params = urllib.parse.quote_plus
('Driver={ODBC Driver 18 for SQL Server};Server=tcp:test-server.database.windows.net,1433;Database=test-database;Uid=*****;Pwd=*****;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine_azure = create_engine(conn_str,echo=True)
connection = engine_azure.connect()
I've tried changing the driver to 'SQL Server' and the driver file path '/opt/homebrew/lib/libmsodbcsql.18.dylib'. None of these have worked.
I am able to connect using the pyodbc.connect() function, so I know my connection string is correct.
In the /opt/homebrew/etc/ folder I have the odbc.ini file which is empty and the odbcinst.ini file which is as follows:
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.18.dylib
UsageCount=1
odbcinst -j gives:
unixODBC 2.3.11
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/wholt2/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
I created shortcuts to from the /etc/ folder to the /opt/homebrew/etc/ folder for the odbc.ini and odbcinst.ini files. Strangely, the ODBCDataSources and .odbc.ini files don't exist. I've tried uninstalling and reinstalling the drivers using brew install msodbcsql18 mssql-tools18
, but nothing seems to work. Any help would be greatly appreciated. Thank you!
2条答案
按热度按时间nsc4cvqm1#
You can try the code below to connect to an Azure SQL database with SQL Alchemy:
This will connect to the database successfully without any errors.
Regarding the error below:
Check the location of the odbc.ini and odbcinst.ini files. If the files are in the
/usr/local/etc/
location, then copy them to the /etc/ location. Use the commands below to copy the files:This may resolve the issue. For more information, you can refer to this .
eufgjt7s2#
I managed to find a workaround for my issue. Thank you for your help everyone!
I wasn't able to get the ODBC drivers to work. I think something is just broken there. I got around the issue by using the PYMSSQL driver instead.
Here's the code
To get it working I had to run these commands in the terminal