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.

import pyodbc
from sqlalchemy import create_engine
import pandas as pd

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

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

Below is the screenshot from SQL Server Management Studio.

Learn with YouTube

How to connect SQLALCHEMY to Microsoft SQL Server Database.

Leave a Reply