-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.py
69 lines (59 loc) · 1.74 KB
/
frontend.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
import mysql.connector
import pandas as pd
# MySQL connection
def connect_mysql():
connection = mysql.connector.connect(
host='localhost',
user='root',
password='2002', # Update with your MySQL password
database='supsql' # Update with your database name
)
return connection
# Fetch data from MySQL
def fetch_data_from_mysql(table_name):
conn = connect_mysql()
query = f"SELECT * FROM {table_name}"
df = pd.read_sql(query, conn)
conn.close()
return df
# Execute custom SQL query
def execute_query(query):
conn = connect_mysql()
cursor = conn.cursor()
try:
cursor.execute(query)
conn.commit()
return "Query executed successfully!"
except Exception as e:
conn.rollback()
return f"Error: {str(e)}"
finally:
cursor.close()
conn.close()
# Streamlit App
def app():
st.title("SQL Database Manager")
# Input for table name
table_name = st.text_input("Enter the table name to display", "sheet1")
# Display current data
st.subheader("Current Data")
try:
df = fetch_data_from_mysql(table_name)
st.dataframe(df)
except Exception as e:
st.error(f"Error fetching data: {str(e)}")
# Input for custom SQL query
st.subheader("Run Custom SQL Query")
sql_query = st.text_area("Enter your SQL query here")
if st.button("Execute Query"):
result = execute_query(sql_query)
if "Error" in result:
st.error(result)
else:
st.success(result)
# Refresh the displayed data after successful query
df = fetch_data_from_mysql(table_name)
st.dataframe(df)
if __name__ == "__main__":
app()