1

I have searched alot for the soloution but still struggling with this problem.

I'm trying to connect to a SQL Server instance running on 127.0.0.1:1433. However, I'm getting a sqlalchemy.exc.DBAPIError with the following error message:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")

I think I need to install the ODBC driver, but I'm not sure if it needs to be installed on the SQL Server Docker image or on my local VM. If the answer is the Docker image, then I think my /etc/odbcinst.ini file is correctly configured as follows:

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.2.1
UsageCount=1

But if the ODBC driver needs to be installed on my local VM, then my /etc/odbcinst.ini file is empty.

Here's the Python code I used to connect to the SQL Server instance:

from sqlalchemy import create_engine

server = "127.0.0.1,1433"
user = "sa"
password = "Pass@12345"
db_name = "test_database"

engine = create_engine(f'mssql+pyodbc://{user}:{password}@{server}/{db_name}?driver=ODBC Driver 17 for SQL Server')

connection = engine.connect()

print("connected")

Another question is what should i do if there is @ in password?

  • sqlserver: sqlserver:2022-latest docker image, runs on 127.0.0.1:1433
  • os: Ubuntu 22.04
  • python: 3.10.6
  • sqlalchemy: 2.0.16

Any help would be greatly appreciated. Thanks!

Mostafa Najmi
  • 315
  • 1
  • 4
  • 11
  • The driver needs to be installed on the device running the code. If you're running that python inside a container, then it needs to be in that container. – Thom A Jul 14 '23 at 08:32
  • thanks. however it will work on a container but currectly i am working on my local machine. – Mostafa Najmi Jul 14 '23 at 08:38
  • Then you need to install the driver on your lcoal machine. – Thom A Jul 14 '23 at 08:39
  • I have tried to install using ODBC driver 18 as [microsoft guide](https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=ubuntu18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline) but getting error `Unable to locate package msodbcsql18` – Mostafa Najmi Jul 14 '23 at 08:42
  • Solved by `sudo apt-get update`. (I have tried it before but due to DNS filtering it may not have been updated correctly) – Mostafa Najmi Jul 14 '23 at 08:55
  • Now getting `Login timeout expired`. I think this is for incorrect connection string which caused by `@` in password. – Mostafa Najmi Jul 14 '23 at 08:57
  • *"I have tried to install using ODBC driver 18"* Your error states you are lookign for ODBC 17; 17 <> 18. – Thom A Jul 14 '23 at 08:59
  • if you believe its due to special characters, can you try https://stackoverflow.com/questions/1423804/writing-a-connection-string-when-password-contains-special-characters ? – python_user Jul 14 '23 at 09:00
  • 1
    Thanks @python_user. using quote_plus solved `Login timeout`. However `SSL certificate error` raise which solved by using `TrustServerCertificate=yes` – Mostafa Najmi Jul 14 '23 at 09:11

1 Answers1

1

Solved by these steps:

1- install ODBC driver on local machine using this script for ubuntu official doc:

if ! [[ "18.04 20.04 22.04 22.10" == *"$(lsb_release -rs)"* ]];
then
    echo "Ubuntu $(lsb_release -rs) is not currently supported.";
    exit;
fi

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev

Note: DNS filtering may cause issues during installation

2- Edit script as bellow to if password contains char @

from sqlalchemy import create_engine
from urllib.parse import quote_plus


server = "127.0.0.1:1433"
user = "sa"
password = "Pass@12345"
db_name = "test_database"
dsn = "ODBC Driver 18 for SQL Server"

engine = create_engine(f"mssql+pyodbc://{user}:%s@{server}/{db_name}?TrustServerCertificate=yes&driver={dsn}" % quote_plus(password))

connection = engine.connect()

print("connected")
Mostafa Najmi
  • 315
  • 1
  • 4
  • 11
  • What are we supposed to do if we are running any of the modern Ubuntu versions? ("18.04 20.04 22.04 22.10") – tier1 Aug 18 '23 at 00:15
  • Visit this page: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=ubuntu18-install%2Calpine17-install%2Cdebian8-install%2Cubuntu16-13-install%2Crhel7-offline#13 – Mostafa Najmi Aug 19 '23 at 09:16