This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain2.py
98 lines (86 loc) · 2.64 KB
/
main2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import streamlit as st
import sqlite3
from sqlite3 import Error
with st.sidebar:
st.subheader("10.10.37.59:8501")
# Function to create a SQLite connection
def create_connection():
try:
conn = sqlite3.connect('django_app.db')
return conn
except Error as e:
st.error(f"Error: '{e}'")
return None
# Function to create the students table if it doesn't exist
def create_table(conn):
try:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
grade TEXT NOT NULL
)
''')
conn.commit()
except Error as e:
st.error(f"Error: '{e}'")
# Function to insert user data into SQLite
def insert_data(conn, name, age, result):
try:
cursor = conn.cursor()
query = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)"
values = (name, age, result)
cursor.execute(query, values)
conn.commit()
st.success("Data added successfully")
except Error as e:
st.error(f"Error: '{e}'")
# Streamlit app code
RESULT_OPTIONS = ["P", "F", "A"]
st.title("Hello There")
name = st.text_input("Enter Your Name")
age = st.number_input("Enter your age", help="Enter your age in this box")
result = st.selectbox("Result", options=RESULT_OPTIONS)
# On button click, insert data into SQLite
if st.button("Add"):
if name and age:
conn = create_connection()
if conn:
create_table(conn)
insert_data(conn, name, int(age), result)
conn.close()
else:
st.warning("Please fill in all the details")
conn = create_connection()
cursor = conn.cursor()
cursor.execute("select * from students")
data = cursor.fetchall()
conn.close()
st.data_editor(data)
st.title("Update")
id_select = st.number_input("Enter the ID")
name_change = st.text_input("Enter the name")
if st.button("Update"):
try:
conn = create_connection()
cursor = conn.cursor()
cursor.execute(f"update students set name='{name_change}' where id={id_select};")
conn.commit()
conn.close()
st.rerun()
except Error as e:
st.error(f"Error: '{e}'")
st.title("Delete")
delete_id = st.number_input("Enter the id to delete")
if st.button("Delete"):
try:
conn = create_connection()
cursor = conn.cursor()
cursor.execute(f"delete from students where id={delete_id};")
conn.commit()
conn.close()
st.rerun()
except Error as e:
st.error(f"Error: '{e}'")