-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
23 lines (19 loc) · 819 Bytes
/
utils.py
File metadata and controls
23 lines (19 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pandas as pd
import sqlalchemy
def print_table(engine, table_name: str):
with engine.connect() as c:
# fetch all rows & pipe into pandas:
res = c.execute(sqlalchemy.text(f"Select * from {table_name}"))
rows = res.fetchall()
# use reflection in SqlAlchemy for getting columns of table:
inspector = sqlalchemy.inspect(engine)
columns = [col.get("name") for col in inspector.get_columns(table_name)]
df = pd.DataFrame.from_records(rows, columns=columns)
print(f"************ Table {table_name} ************".upper())
print(df)
def execute_stmt(engine, stmt):
with engine.connect() as con:
print(f"*** stmt: {stmt}")
rows = con.execute(stmt).fetchall()
df = pd.DataFrame.from_records(rows)
print(df)