Skip to content
Snippets Groups Projects
Commit 7a39c944 authored by Michael Bücker's avatar Michael Bücker
Browse files

Update functions

parent ad23549d
No related branches found
No related tags found
No related merge requests found
Pipeline #161151 passed
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
import psycopg2
import pandas as pd import pandas as pd
import sqlalchemy
from urllib.parse import quote_plus
load_dotenv("/usr/src/app/.env") load_dotenv("/usr/src/app/.env")
class Database:
def __init__(self, db_name):
self.conn = db_connect(db_name)
def __getattr__(self, table_name):
return pd.read_sql_table(table_name, self.conn)
def db_connect(db_name): def db_connect(db_name):
conn = psycopg2.connect( hostname = "wi-sql.fh-muenster.de"
host=os.getenv("DB_HOST"), user = 'digibim'
database=db_name, password = quote_plus("Di&i$IM0815")
user=os.getenv("DB_USER"), conn = sqlalchemy.create_engine(f'postgresql+psycopg2://{user}:{password}@{hostname}/{db_name}')
password=os.getenv("DB_PASSWORD")) return conn
return(conn)
def get_table(db_name, table_name): def get_table(db_name, table_name):
conn = db_connect(db_name) conn = db_connect(db_name)
sql = f"select * from {table_name};" dat = pd.read_sql_table(table_name, conn)
dat = pd.io.sql.read_sql_query(sql, conn) return dat
return(dat)
def get_all_tables(db_name):
db_obj = Database(db_name)
return db_obj
...@@ -3,3 +3,4 @@ jupyterlab-gitlab==3.0.0 ...@@ -3,3 +3,4 @@ jupyterlab-gitlab==3.0.0
psycopg2-binary psycopg2-binary
psycopg2 psycopg2
python-dotenv python-dotenv
sqlalchemy
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment