【仅限前500名开发者】OpenAI发布会技术密钥包:含Model Context Protocol v2规范、Rate Limiting 3.0策略表、Error Code映射速查表
2026/7/1 10:36:14
下面给你一个基于 Python + MySQL 的简易餐厅点餐系统示例,适合课程设计 / 练手项目。特点是:结构清晰、代码少、易扩展。
✅ 菜品管理(增删查)
✅ 顾客下单
✅ 订单查看
✅ 订单结算
CREATE DATABASE restaurant DEFAULT CHARSET utf8mb4; USE restaurant;dishesCREATE TABLE dishes ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, price DECIMAL(6,2) NOT NULL, stock INT DEFAULT 100 );ordersCREATE TABLE orders ( id INT PRIMARY KEY AUTO_INCREMENT, table_no INT NOT NULL, total_price DECIMAL(8,2), create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP );order_itemsCREATE TABLE order_items ( id INT PRIMARY KEY AUTO_INCREMENT, order_id INT, dish_id INT, quantity INT, FOREIGN KEY (order_id) REFERENCES orders(id), FOREIGN KEY (dish_id) REFERENCES dishes(id) );pip install pymysqldb.py
import pymysql def get_conn(): return pymysql.connect( host="localhost", user="root", password="123456", # 改成你的密码 database="restaurant", charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor )from db import get_conn def show_menu(): conn = get_conn() with conn.cursor() as cursor: cursor.execute("SELECT * FROM dishes") dishes = cursor.fetchall() print("\n===== 菜品列表 =====") for d in dishes: print(f"{d['id']}. {d['name']} ¥{d['price']} 库存:{d['stock']}") conn.close()def create_order(table_no): conn = get_conn() try: with conn.cursor() as cursor: cursor.execute( "INSERT INTO orders (table_no) VALUES (%s)", (table_no,) ) order_id = cursor.lastrowid while True: show_menu() dish_id = input("输入菜品ID(0结束):") if dish_id == "0": break qty = int(input("数量:")) cursor.execute( "SELECT price, stock FROM dishes WHERE id=%s", (dish_id,) ) dish = cursor.fetchone() if not dish or dish["stock"] < qty: print("库存不足") continue cursor.execute( "INSERT INTO order_items (order_id,dish_id,quantity) " "VALUES (%s,%s,%s)", (order_id, dish_id, qty) ) cursor.execute( "UPDATE dishes SET stock=stock-%s WHERE id=%s", (qty, dish_id) ) conn.commit() calc_total(order_id) print("下单成功") finally: conn.close()def calc_total(order_id): conn = get_conn() with conn.cursor() as cursor: sql = """ SELECT SUM(d.price*i.quantity) total FROM order_items i JOIN dishes d ON i.dish_id=d.id WHERE i.order_id=%s """ cursor.execute(sql, (order_id,)) total = cursor.fetchone()["total"] cursor.execute( "UPDATE orders SET total_price=%s WHERE id=%s", (total, order_id) ) conn.commit() conn.close()def show_orders(): conn = get_conn() with conn.cursor() as cursor: cursor.execute(""" SELECT o.id,o.table_no,o.total_price,o.create_time, d.name,i.quantity FROM orders o JOIN order_items i ON o.id=i.order_id JOIN dishes d ON i.dish_id=d.id ORDER BY o.id DESC """) for row in cursor.fetchall(): print(row) conn.close()main.py
from order import create_order, show_orders from menu import show_menu def main(): while True: print("\n1. 查看菜单") print("2. 点餐") print("3. 查看订单") print("0. 退出") choice = input("选择:") if choice == "1": show_menu() elif choice == "2": table = int(input("桌号:")) create_order(table) elif choice == "3": show_orders() elif choice == "0": break if __name__ == "__main__": main()===== 菜品列表 ===== 1. 鱼香肉丝 ¥18.00 库存:50 2. 宫保鸡丁 ¥22.00 库存:40 输入菜品ID(0结束):1 数量:2 下单成功✅ 管理员登录
✅ 图形界面(Tkinter / PyQt)
✅ Web 版(Flask / Django)
✅ 支付模拟
✅ 报表统计(日营业额)