Python+MySQL制作一个简易的餐厅点餐系统
2026/7/1 9:21:20 网站建设 项目流程

下面给你一个基于 Python + MySQL 的简易餐厅点餐系统示例,适合课程设计 / 练手项目。特点是:结构清晰、代码少、易扩展


一、功能设计(简易版)

✅ 菜品管理(增删查)

✅ 顾客下单

✅ 订单查看

✅ 订单结算


二、数据库设计(MySQL)

1️⃣ 创建数据库

CREATE DATABASE restaurant DEFAULT CHARSET utf8mb4; USE restaurant;

2️⃣ 菜品表dishes

CREATE TABLE dishes ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, price DECIMAL(6,2) NOT NULL, stock INT DEFAULT 100 );

3️⃣ 订单表orders

CREATE TABLE orders ( id INT PRIMARY KEY AUTO_INCREMENT, table_no INT NOT NULL, total_price DECIMAL(8,2), create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP );

4️⃣ 订单明细表order_items

CREATE 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) );

三、Python 环境准备

pip install pymysql

四、Python 连接 MySQL

db.py

import pymysql def get_conn(): return pymysql.connect( host="localhost", user="root", password="123456", # 改成你的密码 database="restaurant", charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor )

五、核心功能实现

1️⃣ 显示菜单

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()

2️⃣ 下单功能

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()

3️⃣ 计算订单总价

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()

4️⃣ 查看订单

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)

✅ 支付模拟

✅ 报表统计(日营业额)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询