Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect) while connect with sqlalchemy – Sqlalchemy

Ali Hasan
boot2docker flask-sqlalchemy llama-cpp-python mongodbcompass sql-server

Quick Fix: Ensure the ODBC Driver 17 for SQL Server is installed on your local machine. Additionally, if your password contains the ‘@’ character, try using the ‘quote_plus’ function to encode it in your connection string.

The Solutions:

Solution 1: Install ODBC Driver on Local Machine and Handle Password with Special Characters in Connection String

  1. Install ODBC Driver on Local Machine: Use the provided script to install the ODBC driver on your local machine. This script should be executed as root.

  2. Handle Password with Special Characters: By default, sqlalchemy does not handle special characters ("@") in the password correctly. To address this, you can use urllib.parse.quote_plus function to encode the password before connecting.

The modified Python code:

from sqlalchemy import create_engine
from urllib.parse import quote_plus

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

# Encode the password to handle special characters
encoded_password = quote_plus(password)

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

connection = engine.connect()



How to handle password which contains @ char in sqlalchemy connection?

Use quote_plus in password value of sqlalchemy connection string.

Where I have to install the ODBC driver?

ODBC drive should be installed on your local VM, not on the SQL server docker image.

Video Explanation:

The following video, titled "Jennifer Lopez - Can't Get Enough (Official Music Video) - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.