sampathlonka commited on
Commit
6ec8cbf
·
verified ·
1 Parent(s): 36c8d1b

Upload database

Browse files
Files changed (1) hide show
  1. database.py +45 -0
database.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pymysql
2
+ 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["user"],
13
+ "password": st.secrets["password"],
14
+ "port": 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()
22
+ cursor = db.cursor()
23
+ try:
24
+ cursor.execute(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:
34
+ db.close()
35
+
36
+
37
+ def _get_details_mantra_json(self, query):
38
+ description, data = execute_query(query)
39
+ df = pd.DataFrame(data)
40
+ df.columns = [x[0] for x in description]
41
+ mantra_json = df['mantra_json'].values[0]
42
+ cleaned_data = re.sub('<[^<]+?>', '', mantra_json)
43
+ return json.loads(cleaned_data)
44
+
45
+