Created
March 2, 2023 19:08
-
-
Save aymenkrifa/6b41283027b86cc6918a92170604c3bd to your computer and use it in GitHub Desktop.
Establish a database connection using SQLAlchemy with Pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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