-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_utils.py
More file actions
31 lines (23 loc) · 953 Bytes
/
sql_utils.py
File metadata and controls
31 lines (23 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from sqlalchemy import create_engine
import pandas.io.sql as sqlio
import os
from dotenv import load_dotenv
def get_engine(protocol=None, user=None, password=None, host=None, port=None, db=None):
load_dotenv()
protocol = protocol if protocol else 'postgresql+psycopg2'
user = user if user else os.environ.get('POSTGRES_USER')
password = password if password else os.environ.get('POSTGRES_PASSWORD')
host = host if host else 'localhost'
port = port if port else 5432
db = db if db else os.environ.get('POSTGRES_DB')
db_url = f'{protocol}://{user}:{password}@{host}:{port}/{db}'
engine = create_engine(db_url)
return engine
def make_read_query_func(engine):
def read_query(query, verbose=True):
if verbose:
print(query, '\n')
with engine.connect() as conn:
df = sqlio.read_sql_query(query, conn)
return df
return read_query