diff --git "a/32 \346\236\227\344\275\263\346\200\241/20260623 \345\256\214\346\225\264\347\232\204\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/32 \346\236\227\344\275\263\346\200\241/20260623 \345\256\214\346\225\264\347\232\204\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..566ab1f419ea5fec97db07abb3827993b7796279 --- /dev/null +++ "b/32 \346\236\227\344\275\263\346\200\241/20260623 \345\256\214\346\225\264\347\232\204\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,110 @@ +``` +import streamlit as st +import pandas as pd +import sqlite3 + +st.set_page_config(page_title="图书管理系统") + +def init_db(): + conn = sqlite3.connect('library.db') + conn.execute(""" + CREATE TABLE IF NOT EXISTS books( + id INTEGER PRIMARY KEY AUTOINCREMENT, + book_name TEXT NOT NULL, + author TEXT NOT NULL, + price REAL NOT NULL + ) + """) + conn.commit() + conn.close() +def add_book(book_name, author, price): + conn = sqlite3.connect('library.db') + conn.execute( + "INSERT INTO books (book_name, author, price) VALUES (?, ?, ?)", + (book_name, author, price) + ) + conn.commit() + conn.close() +def delete_book(id): + # 3.1.创建连接 + conn = sqlite3.connect("library.db") + # 3.2.执行SQL + conn.execute("delete from books where id = ?", (id,)) + # 3.3.提交 + conn.commit() + # 3.4.关闭连接 + conn.close() +# 查询所有图书 +def show_books(): + conn = sqlite3.connect('library.db') + data = conn.execute("SELECT * FROM books") + df = pd.DataFrame(data, columns=["id", "book_name", "author", "price"]) + conn.close() + return df + +# 初始化数据表 +init_db() + +# 登录状态 +if "logged_in" not in st.session_state: + st.session_state.logged_in = False + +# 登录页面 +if not st.session_state.logged_in: + st.title("图书管理系统") + st.subheader("用户登录") + with st.form("login_form"): + username = st.text_input("用户名", placeholder="请输入用户名") + password = st.text_input("密码", placeholder="请输入密码", type="password") + login_btn = st.form_submit_button("登录", use_container_width=True) + if login_btn: + if username == "admin" and password == "666": + st.session_state.logged_in = True + st.success("登录成功!") + st.rerun() + else: + st.error("用户名或密码错误!") + st.stop() + +# 登录后侧边菜单 +with st.sidebar: + st.title("=== 菜单 ===") + st.markdown("---") + menu = st.selectbox("请选择功能", ["查看所有图书", "添加图书"]) + +if menu == "查看所有图书": + st.title("📚 图书列表") + st.markdown("---") + books = show_books() + if len(books)>0: + cols = st.columns([1,5,3,1,2]) + cols[0].write("序号") + cols[1].write("书名") + cols[2].write("作者") + cols[3].write("价格") + cols[4].write("操作") + for idx, book in books.iterrows(): + cols = st.columns([1,5,3,1,2]) + cols[0].write(idx + 1) + cols[1].write(book["book_name"]) + cols[2].write(book["author"]) + cols[3].write(book["price"]) + cols[4].button("删除",key=f"{idx}",on_click=delete_book,args=(book["id"],)) + +elif menu == "添加图书": + st.title(" 添加新图书") + st.markdown("---") + with st.form("add_book_form", clear_on_submit=True): + name = st.text_input("书名") + author = st.text_input("作者") + price = st.number_input("价格") + submit = st.form_submit_button("提交添加") + if submit: + + if not name or not author: + st.warning("书名和作者不能为空!") + else: + add_book(name, author, price) + st.success("图书添加成功!") + st.rerun() +``` \ No newline at end of file