import streamlit as stimport sqlite3import pandas as pd# Database functionsconn = sqlite3.connect('contacts.db')c = conn.cursor()def create_table(): c.execute('CREATE TABLE IF NOT EXISTS contact (name TEXT, email TEXT, phone TEXT, address TEXT)')def add_data(name, email, phone, address): c.execute('INSERT INTO contact (name, email, phone, address) VALUES (?, ?, ?, ?)', (name, email, phone, address)) conn.commit()def view_all_data(): c.execute('SELECT * FROM contact') data = c.fetchall() return datadef view_all_names(): c.execute('SELECT DISTINCT name FROM contact') data = c.fetchall() return datadef get_data_by_name(name): c.execute('SELECT * FROM contact WHERE name LIKE ?', ('%'+name+'%',)) data = c.fetchall() return datadef delete_data(name): c.execute('DELETE FROM contact WHERE name=?', (name,)) conn.commit()def update_data(name, email, phone, address, old_name): c.execute('UPDATE contact SET name=?, email=?, phone=?, address=? WHERE name=?', (name, email, phone, address, old_name)) conn.commit()# UI functionsdef create(): st.subheader("Create New Contact") with st.form("Create Form"): name = st.text_input("Name") email = st.text_input("Email") phone = st.text_input("Phone") address = st.text_input("Address") submit_button = st.form_submit_button("Add Contact") if submit_button: create_table() add_data(name, email, phone, address) st.success("Successfully Added Contact Into Database") st.form_clear_on_submit()def read(): st.subheader("Search Contacts") search_term = st.text_input("Enter Name") search_button = st.button("Search") clear_button = st.button("Clear") if search_button: result = get_data_by_name(search_term) for i in result: st.write(i) if clear_button: st.empty()def update(): st.subheader("Update Contact") with st.form("Update Form"): old_name = st.selectbox("Name", options=view_all_names()) name = st.text_input("New Name", value=old_name) email = st.text_input("New Email") phone = st.text_input("New Phone") address = st.text_input("New Address") update_button = st.form_submit_button("Update Contact") if update_button: update_data(name, email, phone, address, old_name) st.success("Successfully Updated Contact")def delete(): st.subheader("Delete Contact") name_to_delete = st.selectbox("Select Name", options=view_all_names()) if st.button("Delete Contact"): delete_data(name_to_delete) st.success("Successfully Deleted Contact")def main(): st.title("Contact Management System") menu = ["Create", "Read", "Update", "Delete", "Quit"] choice = st.sidebar.radio("Menu", menu) if choice == "Create": create() elif choice == "Read": read() elif choice == "Update": update() elif choice == "Delete": delete() elif choice == "Quit": st.balloons() # Display the top 10 rows of the database st.subheader("Top 10 Contacts") df = pd.DataFrame(view_all_data(), columns=["Name", "Email", "Phone", "Address"]) st.dataframe(df.head(10))if __name__ == "__main__": main()
希望以上信息能对您有所帮助!如果还有其他问题,请随时提问。