SQL Server to Python with SQLAlchemy


Do you need to bring data in from a Microsoft SQL Server database into a Python project? This is the code for a Python project I have created in Jupyter Notebook with Anaconda Navigator.

1import pyodbc
2from sqlalchemy import create_engine
3import pandas as pd

1Server='localhost'
2Database='MikeTest'
3Driver='ODBC Driver 17 for SQL Server'
4Database_Con = f'mssql://@{Server}/{Database}?driver={Driver}'
5#
6engine = create_engine(Database_Con)
7con = engine.connect()

1TableName = 'Names3'
2df=pd.read_sql_query(f'SELECT * FROM {TableName}',con)
3df

Below is the screenshot from SQL Server Management Studio.

Learn with YouTube

How to connect SQLALCHEMY to Microsoft SQL Server Database.

Leave a Reply