Skip to content

Instantly share code, notes, and snippets.

@dineshkumarkb
Created September 20, 2022 14:40
Show Gist options
  • Select an option

  • Save dineshkumarkb/d21da8b3794eb89348eda334bd63a2ae to your computer and use it in GitHub Desktop.

Select an option

Save dineshkumarkb/d21da8b3794eb89348eda334bd63a2ae to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from urllib.parse import quote
def connect_to_db():
connection_url = "mysql+pymysql://<username>:<password>@<hostname>:3306/<databasename>"
try:
engine = create_engine(url=connection_url)
# create a session
session = sessionmaker(bind=engine)
conn = session()
if session:
return conn
except SQLAlchemyError as se:
print(se)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment