The Solutions:
Solution 1: Install ODBC Driver on Local Machine and Handle Password with Special Characters in Connection String
-
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.
-
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 = "127.0.0.1:1433"
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()
print("connected")
Q&A
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.
This video provides further insights and detailed explanations related to the content discussed in the article.
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.
This video provides further insights and detailed explanations related to the content discussed in the article.