sampathlonka commited on
Commit
16d2e29
1 Parent(s): 334eff5

update database

Browse files
Files changed (1) hide show
  1. database.py +19 -10
database.py CHANGED
@@ -3,19 +3,27 @@ import json
3
  import pandas as pd
4
  import re
5
  import streamlit as st
 
6
 
 
 
7
 
8
  # Database connection
9
  def initialize_database():
10
- # Database Connection
11
- db_params = {"host": st.secrets["host"],
12
- "user": st.secrets["username"],
13
- "password": st.secrets["password"],
14
- "port": int(st.secrets["port"]),
15
- "database": st.secrets["database"]
16
- }
17
- db = pymysql.connect(**db_params)
18
- return db
 
 
 
 
 
19
 
20
  def execute_query(query):
21
  db = initialize_database()
@@ -25,9 +33,10 @@ def execute_query(query):
25
  description = cursor.description
26
  result = cursor.fetchall() # Fetch all rows from the result set
27
  db.commit()
 
28
  return description, result
29
  except Exception as e:
30
- print("Error executing query:", e)
31
  db.rollback()
32
  return None # Return None if an error occurs
33
  finally:
 
3
  import pandas as pd
4
  import re
5
  import streamlit as st
6
+ import logging
7
 
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO) # Set logging level to INFO
10
 
11
  # Database connection
12
  def initialize_database():
13
+ try:
14
+ # Database Connection
15
+ db_params = {"host": st.secrets["host"],
16
+ "user": st.secrets["username"],
17
+ "password": st.secrets["password"],
18
+ "port": int(st.secrets["port"]),
19
+ "database": st.secrets["database"]
20
+ }
21
+ db = pymysql.connect(**db_params)
22
+ logging.info("Connected to the database successfully!")
23
+ return db
24
+ except pymysql.MySQLError as e:
25
+ logging.error("Error connecting to the database: %s", e)
26
+ raise # Re-raise the exception to propagate it up the call stack
27
 
28
  def execute_query(query):
29
  db = initialize_database()
 
33
  description = cursor.description
34
  result = cursor.fetchall() # Fetch all rows from the result set
35
  db.commit()
36
+ logging.info("Query executed successfully: %s", query)
37
  return description, result
38
  except Exception as e:
39
+ logging.error("Error executing query: %s", e)
40
  db.rollback()
41
  return None # Return None if an error occurs
42
  finally: