SQLAlchemy does not connect to SQL Server with ODBC Driver 18 for SQL Server but pyodbc does

preview_player
Показать описание

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!SQLAlchemy does not connect to SQL Server with ODBC Driver 18 for SQL Server but pyodbc does

I am working on a windows pc and cannot connect to my Microsoft SQL server with SQLAlchemy.
I have the login credentials as follows: IP, Server name, user name, database, password.
I can't use windows authorization since I want to run the finished script off an r, pi or cloud service periodically throughout the day.
password = 'xxx'
driver='{ODBC Driver 18 for SQL Server}' #does not work with pyodbc if + used instead of {}

connection_string = f"mssql+pyodbc://{username}:{password}@{server}/{database}?driver={driver};Encrypt=False"

try:
# Create the SQLAlchemy engine
engine = create_engine(connection_string)

# Test the connection
# Use the inspector to get table names
inspector = inspect(connection)

# Print the list of tables
print("Tables in the database:")
for table in tables:
print(table)

except Exception as e:
print(f"Error connecting to the database: {e}")

password = 'xxx'
driver='{ODBC Driver 18 for SQL Server}' #does not work with pyodbc if + used instead of {}

connection_string = f"mssql+pyodbc://{username}:{password}@{server}/{database}?driver={driver};Encrypt=False"

try:
# Create the SQLAlchemy engine
engine = create_engine(connection_string)

# Test the connection
# Use the inspector to get table names
inspector = inspect(connection)

# Print the list of tables
print("Tables in the database:")
for table in tables:
print(table)

except Exception as e:
print(f"Error connecting to the database: {e}")

I am able to connect to the server without issue using pyodbc only.
connection_string = f'DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password};TrustServerCertificate=Yes;'

try:
print("Connection successful!")
except Exception as e:
print(f"Error connecting to the database: {e}")

connection_string = f'DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password};TrustServerCertificate=Yes;'

try:
print("Connection successful!")
except Exception as e:
print(f"Error connecting to the database: {e}")

When trying to use SQLAlchemy I either get an 08001, IM0002 or IM0012 Error depending on what I do to the connect string.
In driver='{ODBC Driver 18 for SQL Server}' I have also tried + instead of spaces with and without {}
driver='{ODBC Driver 18 for SQL Server}'
+
{}
I have also tried with and without TrustedServerCertificate=Yes/True and Encrypt=False/No
TrustedServerCertificate=Yes/True
Encrypt=False/No
I expected that SQLAlchemy would have connected as easily pyodbc but I can't get it to work.
I also tried the method suggested here.
here
Full error:

Error connecting to the database: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')

Error connecting to the databSource of the question:

Question and source license information:
Рекомендации по теме
welcome to shbcf.ru