Spaces:
Sleeping
Sleeping
update database
Browse files- 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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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:
|