|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import mysql.connector |
|
import streamlit as st |
|
from mysql import connector |
|
|
|
|
|
|
|
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 users2") |
|
result = mycursor.fetchall() |
|
for row in result: |
|
st.write(row) |
|
|
|
|
|
|
|
|
|
def main(): |
|
st.title("CRUD Operations With MySQL"); |
|
|
|
|
|
option=st.sidebar.selectbox("Select an Operation",("Create","Read","Update","Delete")) |
|
|
|
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 name=%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 users2 where id =%s" |
|
val=(id,) |
|
mycursor.execute(sql,val) |
|
mydb.commit() |
|
st.success("Record Deleted Successfully!!!") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|