File size: 2,468 Bytes
95ea996 7ba3fbf 95ea996 96262b5 95ea996 9180c63 ae8156b 95ea996 66957d1 95ea996 66957d1 95ea996 2531a6f 8ccd9d7 95ea996 66957d1 ac44277 95ea996 |
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 |
# https://www.youtube.com/watch?v=T6lMgciw8o8&t=232s
# create table users (id integer primary key auto_increment, name varchar(50), email varchar(50))
# use sampledb;
# CREATE TABLE users (
# id int NOT NULL auto_increment,
# name varchar(50),
# email varchar(50),
# primary key (id)
# )
import mysql.connector
import streamlit as st
from mysql import connector
# Establish a connection to MySQL Server
mydb = connector.connect(
host="database-1.cs0xk0lbm7wp.ap-southeast-2.rds.amazonaws.com",
user="admin",
password="yY10130627",
database="sampledb"
)
mycursor=mydb.cursor()
st.write("Connection Established")
# mycursor.execute("select * from mytable")
mycursor.execute("select * from users2")
result = mycursor.fetchall()
for row in result:
st.write(row)
# Create Streamlit App
def main():
st.title("CRUD Operations With MySQL");
# Display Options for CRUD Operations
option=st.sidebar.selectbox("Select an Operation",("Create","Read","Update","Delete"))
# Perform Selected CRUD Operations
if option=="Create":
st.subheader("Create a Record")
name=st.text_input("Enter Name")
email=st.text_input("Enter Email")
if st.button("Create"):
sql= "insert into users2(name,email) values(%s,%s)"
val= (name,email)
mycursor.execute(sql,val)
mydb.commit()
st.success("Record Created Successfully!!!")
elif option=="Read":
st.subheader("Read Records")
mycursor.execute("select * from users2")
result = mycursor.fetchall()
for row in result:
st.write(row)
elif option=="Update":
st.subheader("Update a Record")
id=st.number_input("Enter ID")
name=st.text_input("Enter Name")
email=st.text_input("Enter New Email")
if st.button("Update"):
sql="update users2 set email=%s, email=%s where id=%s"
val=(name,email, id)
mycursor.execute(sql)
mydb.commit()
st.success("Record Updated Successfully!!!")
elif option=="Delete":
st.subheader("Delete a Record")
id=st.number_input("Enter ID",min_value=1)
if st.button("Delete"):
sql="delete from users where id =%s"
val=(id,)
mycursor.execute(sql,val)
mydb.commit()
st.success("Record Deleted Successfully!!!")
if __name__ == "__main__":
main()
|