Commit
·
95ea996
1
Parent(s):
703296e
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://www.youtube.com/watch?v=T6lMgciw8o8&t=232s
|
| 2 |
+
import mysql.connector
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from mysql import connector
|
| 5 |
+
|
| 6 |
+
# Establish a connection to MySQL Server
|
| 7 |
+
|
| 8 |
+
mydb = connector.connect(
|
| 9 |
+
host="database-1.cs0xk0lbm7wp.ap-southeast-2.rds.amazonaws.com",
|
| 10 |
+
user="admin",
|
| 11 |
+
password="yY10130627",
|
| 12 |
+
database="pets"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
mycursor=mydb.cursor()
|
| 17 |
+
st.write("Connection Established")
|
| 18 |
+
mycursor.execute("select * from mytable")
|
| 19 |
+
result = mycursor.fetchall()
|
| 20 |
+
for row in result:
|
| 21 |
+
st.write(row)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Create Streamlit App
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
st.title("CRUD Operations With MySQL");
|
| 28 |
+
|
| 29 |
+
# Display Options for CRUD Operations
|
| 30 |
+
option=st.sidebar.selectbox("Select an Operation",("Create","Read","Update","Delete"))
|
| 31 |
+
# Perform Selected CRUD Operations
|
| 32 |
+
if option=="Create":
|
| 33 |
+
st.subheader("Create a Record")
|
| 34 |
+
name=st.text_input("Enter Name")
|
| 35 |
+
email=st.text_input("Enter Email")
|
| 36 |
+
if st.button("Create"):
|
| 37 |
+
sql= "insert into users(name,email) values(%s,%s)"
|
| 38 |
+
val= (name,email)
|
| 39 |
+
mycursor.execute(sql,val)
|
| 40 |
+
mydb.commit()
|
| 41 |
+
st.success("Record Created Successfully!!!")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
elif option=="Read":
|
| 46 |
+
st.subheader("Read Records")
|
| 47 |
+
mycursor.execute("select * from users")
|
| 48 |
+
result = mycursor.fetchall()
|
| 49 |
+
for row in result:
|
| 50 |
+
st.write(row)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
elif option=="Update":
|
| 55 |
+
st.subheader("Update a Record")
|
| 56 |
+
id=st.number_input("Enter ID",min_value=1)
|
| 57 |
+
name=st.text_input("Enter New Name")
|
| 58 |
+
email=st.text_input("Enter New Email")
|
| 59 |
+
if st.button("Update"):
|
| 60 |
+
sql="update users set name=%s, email=%s where id =%s"
|
| 61 |
+
val=(name,email,id)
|
| 62 |
+
mycursor.execute(sql,val)
|
| 63 |
+
mydb.commit()
|
| 64 |
+
st.success("Record Updated Successfully!!!")
|
| 65 |
+
|
| 66 |
+
elif option=="Delete":
|
| 67 |
+
st.subheader("Delete a Record")
|
| 68 |
+
id=st.number_input("Enter ID",min_value=1)
|
| 69 |
+
if st.button("Delete"):
|
| 70 |
+
sql="delete from users where id =%s"
|
| 71 |
+
val=(id,)
|
| 72 |
+
mycursor.execute(sql,val)
|
| 73 |
+
mydb.commit()
|
| 74 |
+
st.success("Record Deleted Successfully!!!")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|