-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbfunctions.py
36 lines (32 loc) · 898 Bytes
/
dbfunctions.py
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
32
33
34
35
36
import streamlit as st
import psycopg2
import pandas as pd
# Create a connection to database
#@st.cache_resource
def connectDatabase():
conn = psycopg2.connect(
host=st.secrets["DBHOST"],
database=st.secrets["DBNAME"],
user=st.secrets["DBUSER"],
password=st.secrets["DBPASSWORD"],
port=st.secrets["DBPORT"],)
conn.autocommit = True
cursor = conn.cursor()
return cursor
def executeQuery(query):
conn = connectDatabase()
conn.execute(query)
result = conn.fetchall()
return result
def executeWithoutFetch(query):
conn = connectDatabase()
conn.execute(query)
return None
#@st.cache_data(ttl=30)
def loadTable(query):
conn = connectDatabase()
conn.execute(query)
result = conn.fetchall()
colnames = [desc[0] for desc in conn.description]
df = pd.DataFrame(result, columns=colnames)
return df