Connect to MSSQL Server with Python using SQLAlChemy

7590
1
06-22-2023 07:15 AM
ChallagundlaSindhuraITS
Emerging Contributor

I have installed SQLAlchemy 2.0.16 on my machine. I am trying to connect to sql server database using below code:

from sqlalchemy import text , create_engine
Server = ''
Database = ''
Driver = ''
Database_Con = f'mssql://@{Server}/{Database}?driver={Driver}'
#Create the engine to connect to the database
engine=create_engine(Database_Con)
con = engine.connect()

Throws ModuleNotFoundError: No module named 'pyodbc'

If I do a pip install pyodc it works. But my team doesnt want me to install pyodc and want to try to connect to SQLengine. Please let me know any alternatives. Thank You

0 Kudos
1 Reply
EarlMedina
Esri Regular Contributor

Hi,

Seems like your problem is related to create_engine. pyodbc is used as the default DBAPI. You may also use pymssql. You can learn more about this here: https://docs.sqlalchemy.org/en/20/core/engines.html

I would explain to your team that's just how things work. You need some kind of DBAPI and here you have two options.

0 Kudos