Skip to content

Instantly share code, notes, and snippets.

@aymenkrifa
Created March 2, 2023 19:08
Show Gist options
  • Save aymenkrifa/6b41283027b86cc6918a92170604c3bd to your computer and use it in GitHub Desktop.
Save aymenkrifa/6b41283027b86cc6918a92170604c3bd to your computer and use it in GitHub Desktop.
Establish a database connection using SQLAlchemy with Pandas
import pandas as pd
from sqlalchemy import create_engine, URL
from sqlalchemy.sql import text
url_object = URL.create(
drivername="DIALECT+DRIVER",
username="username",
password="password",
host="host",
database="database_name",
)
engine = create_engine(url=url_object)
with engine.connect() as db_conn:
sql_query = "SELECT COUNT(*) FROM table_name"
df = pd.read_sql_query(sql=text(sql_query), con=db_conn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment