Connecting to SQL server using SQLAlchemy

import urllib
import sqlalchemy
import pandas as pd

sql_query = 'SELECT * FROM <tabl-name>'
server = '<sql-server-hostname>'
database = '<sql-server-name>'
username = '<login-id>'
password = '<password>'
driver = '{ODBC Driver 17 for SQL Server}'
odbc_str = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;UID='+username+';DATABASE='+ database + ';PWD='+ password
connect_str = 'mssql+pyodbc:///?odbc_connect=' + urllib.parse.quote_plus(odbc_str)
engine = sqlalchemy.create_engine(connect_str)
df = pd.read_sql(sql_query, engine)
Thanks for reading and happy learning!

Leave a Reply 0

Your email address will not be published. Required fields are marked *