数据库课设--基于Python+MySQL的餐厅点餐系统

文章目录

  • 一、系统需求分析
  • 二、系统设计
    • 1. 功能结构设计
    • 2、概念设计
      • 2.2.1 bill_food表E-R图
      • 2.2.2 bills表E-R图
      • 2.2.3 categories E-R图
      • 2.2.4 discounts表 E-R图
      • 2.2.5 emp表E-R图
      • 2.2.6 food 表E-R图
      • 2.2.7 member表E-R图
      • 2.2.8 member_point_bill表E-R图
      • 2.2.9 servers表E-R图
      • 2.2.10 tables表E-R图
      • 2.2.11 user表E-R图
    • 3. 逻辑设计(表的设计)
      • ①bill_food表
      • ②bills表
      • ③categories表
      • ④discounts表
      • ⑤emp表
      • ⑥food表
      • ⑦member表
      • ⑧member_point_bills表
      • ⑨servers表
      • ⑩tables表
      • 11.user表
  • 三、 系统实现(主要体现以下几部分)
    • 1、系统采用的技术、方法、工具
    • 2、效果图
    • 3、实现代码
      • 2.1 main方法
      • 2.2 后台登录页面
      • 2.3 统计页面
      • 2.4 点餐系统
      • 2.5 后台页面
      • 2.6 统计系统
      • 2.7 登录页面连接数据库
      • 2.8 其他页面连接数据库
  • 四、源码获取

一、系统需求分析

需求分析首先要确定研究分析的对象,这里的需求分析对象有两方:买家和卖家。

对于买家,需求是能够进行线上点餐,具体可以细化为:能够在线上获得餐品信息和进行点餐行为。买家作为消费者,都想得到更为优质的服务体验,希望能够通过较为简单顺利的操作就可以吃到满足自己口味的菜肴。这就要求系统界面能够生动形象地有效呈现出点餐的各项信息,菜品的价格,可以选择就坐的餐桌的数目等情况以及加餐时简洁的操作界面。

对于卖家,需求是能够对菜品进行增查删改以及增加会员,具体可以细化为,能够线上获得全部的菜品信息,添加菜品,修改菜品价格,删除菜品,增加会员。卖家即为商家,商家要能够高效地获取这些的信息,当然是图形化界面和几何形式的信息呈现最为直接明了。

二、系统设计

1. 功能结构设计

在这里插入图片描述

2、概念设计

2.2.1 bill_food表E-R图

在这里插入图片描述

2.2.2 bills表E-R图

在这里插入图片描述

2.2.3 categories E-R图

在这里插入图片描述

2.2.4 discounts表 E-R图

在这里插入图片描述

2.2.5 emp表E-R图

在这里插入图片描述

2.2.6 food 表E-R图

在这里插入图片描述

2.2.7 member表E-R图

在这里插入图片描述

2.2.8 member_point_bill表E-R图

在这里插入图片描述

2.2.9 servers表E-R图

在这里插入图片描述

2.2.10 tables表E-R图

在这里插入图片描述

2.2.11 user表E-R图

在这里插入图片描述

3. 逻辑设计(表的设计)

①bill_food表

CREATE TABLE `bill_food` (
  `id_food` int NOT NULL,
  `id_bill` int NOT NULL,
  `num` int NOT NULL,
  PRIMARY KEY (`id_food`,`id_bill`),
  KEY `FK_bill_food2` (`id_bill`),
  CONSTRAINT `FK_bill_food` FOREIGN KEY (`id_food`) REFERENCES `food` (`id_food`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `FK_bill_food2` FOREIGN KEY (`id_bill`) REFERENCES `bills` (`id_bill`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

②bills表

CREATE TABLE `bills` (
  `id_bill` int NOT NULL,
  `id_table` int NOT NULL,
  `id_member` int DEFAULT NULL,
  `time_order` datetime NOT NULL,
  `time_pay` datetime DEFAULT NULL,
  `money` int NOT NULL,
  PRIMARY KEY (`id_bill`),
  KEY `FK_bill_member` (`id_member`),
  KEY `FK_table_bill` (`id_table`),
  KEY `time_order` (`time_order`),
  CONSTRAINT `FK_bill_member` FOREIGN KEY (`id_member`) REFERENCES `member` (`id_member`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `FK_table_bill` FOREIGN KEY (`id_table`) REFERENCES `tables` (`id_table`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

③categories表

CREATE TABLE `categories` (
  `category` char(20) NOT NULL,
  PRIMARY KEY (`category`)
);

在这里插入图片描述

④discounts表

CREATE TABLE `discounts` (
  `id_discount` int NOT NULL,
  `off_price` int NOT NULL,
  `require_points` int NOT NULL,
  PRIMARY KEY (`id_discount`)
) ;

在这里插入图片描述

⑤emp表

CREATE TABLE `emp` (
  `id_emp` int NOT NULL,
  `id_server` int DEFAULT NULL,
  `name_emp` char(20) NOT NULL,
  `sex_emp` char(1) DEFAULT NULL,
  `phone_num` char(11) DEFAULT NULL,
  `position` char(20) DEFAULT NULL,
  PRIMARY KEY (`id_emp`)
);

在这里插入图片描述

⑥food表

CREATE TABLE `food` (
  `id_food` int NOT NULL,
  `category` char(20) NOT NULL,
  `name_food` char(20) NOT NULL,
  `introduction` char(100) DEFAULT NULL,
  `price` int NOT NULL,
  `url` char(100) DEFAULT NULL,
  PRIMARY KEY (`id_food`),
  KEY `FK_food_category` (`category`),
  CONSTRAINT `FK_food_category` FOREIGN KEY (`category`) REFERENCES `categories` (`category`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

⑦member表

CREATE TABLE `member` (
  `id_member` int NOT NULL,
  `name_member` char(20) DEFAULT NULL,
  `points` int NOT NULL,
  `sex` char(1) DEFAULT NULL,
  `phone_num` char(11) DEFAULT NULL,
  PRIMARY KEY (`id_member`)
) ;

在这里插入图片描述

⑧member_point_bills表

CREATE TABLE `member_point_bill` (
  `id_point_bill` int NOT NULL,
  `id_member` int NOT NULL,
  `time_point` datetime NOT NULL,
  `point` int NOT NULL,
  `note` char(20) DEFAULT NULL,
  PRIMARY KEY (`id_point_bill`),
  KEY `FK_member_point_bill` (`id_member`),
  CONSTRAINT `FK_member_point_bill` FOREIGN KEY (`id_member`) REFERENCES `member` (`id_member`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

⑨servers表

CREATE TABLE `servers` (
  `id_server` int NOT NULL,
  `id_emp` int NOT NULL,
  PRIMARY KEY (`id_server`),
  KEY `FK_to_server` (`id_emp`),
  CONSTRAINT `FK_to_server` FOREIGN KEY (`id_emp`) REFERENCES `emp` (`id_emp`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

⑩tables表

CREATE TABLE `tables` (
  `id_table` int NOT NULL,
  `id_server` int NOT NULL,
  `num_people` int NOT NULL,
  `id_bill` int DEFAULT NULL,
  `id_member` int DEFAULT NULL,
  PRIMARY KEY (`id_table`),
  KEY `FK_server_table` (`id_server`),
  CONSTRAINT `FK_server_table` FOREIGN KEY (`id_server`) REFERENCES `servers` (`id_server`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ;

在这里插入图片描述

11.user表

CREATE TABLE `user` (
  `user_id` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `user_password` varchar(255) NOT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `user_position` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
);

三、 系统实现(主要体现以下几部分)

1、系统采用的技术、方法、工具

餐厅点餐系统采用pycharm tkinter库实现可视化,数据库管理用MySQL

2、效果图

在这里插入图片描述
在这里插入图片描述
其他图就不展示了

3、实现代码

2.1 main方法

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False

# window
import tkinter.messagebox
import tkinter as tk  # 使用Tkinter前需要先导入
import order
import statistic
from database import *
import reig_manage

table = db_get_table()

    #实例化
window_table = tk.Tk()
window_table.title('选择餐桌')
window_table.geometry('300x400')
window_table['bg']='#d0c0c0'
listbox_table = tk.Listbox(window_table, listvariable = table)
#listbox_table['bg']='#9ea4b8'
for table_item in table:
    listbox_table.insert("end", table_item+" "+table[table_item][0])

listbox_table.pack()
#
e_member = tk.Entry(window_table, show=None, font=('Arial', 14))
e_member.insert(0,"输入会员号")
e_member['bg']='#f8f0e0'
e_member.pack()

def submit_table():
    if listbox_table.curselection() == ():
        tkinter.messagebox.showinfo(title='警告', message='请点击框内餐桌再选择')
        return
    table_item = listbox_table.get(listbox_table.curselection())
    occupied = table[table_item[0:3]][0]
    if occupied=="占用":
        tkinter.messagebox.showinfo(title='警告', message='当前餐桌有人')
        return
    table_num = int(table_item[2:3])
    next_bill = db_sit(table_num)
    window_table.withdraw()
    member_id = e_member.get()
    if is_number(member_id):
        member_id = int(member_id)
    else:
        member_id = None
    order.open_order_table(table_num,next_bill,member_id)

def enter_statistic():
    #window_table.withdraw()
    statistic.open()

#登录
def enter_manage():
   # window_table.withdraw()

    reig_manage.register_manage()

#打样
def close_shop():
    db_clear_table()
    table = db_get_table()
    listbox_table.delete(0,"end")
    for table_item in table:
        listbox_table.insert("end", table_item+" "+table[table_item][0])

button_select_table = tk.Button(window_table, text='选择', width=15, height=2, command=submit_table)
button_select_table.pack()
button_statistic = tk.Button(window_table, text='进入后台统计界面', width=15, height=2, command=enter_statistic)
button_statistic.pack()
button_statistic = tk.Button(window_table, text='管理人员登录', width=15, height=2, command=enter_manage)
button_statistic.pack()
button_statistic = tk.Button(window_table, text='打烊', width=15, height=2, command=close_shop)
button_statistic.pack()

# 第7步,主窗口循环显示
window_table.mainloop()

2.2 后台登录页面

import tkinter as tk  # 使用Tkinter前需要先导入
from tkinter import messagebox
from database_manage import *
import manage
from tkinter import *

def register_manage():
    window = tk.Toplevel()
    window.title('后台登录页面')
    window['bg'] = '#d0c0c0'
    window.geometry('300x300')

    Label(window, text='管理人员登录').grid(row=0, column=0, columnspan=2)
    Label(window, text='用户名:').grid(row=1, column=0)
    name = Entry(window)
    name.grid(row=1, column=1)
    Label(window, text='密码:').grid(row=2, column=0, sticky=E)
    passwd = Entry(window, show='*')
    passwd.grid(row=2, column=1)

    def successful():
        falg=db_get_user111(name,passwd)
        if falg==1:
            window.destroy()
            manage.show()
        else:
            messagebox.showerror(title='wrong', message='登录失败,用户名或密码错误')

    Button(window, text='登录', command=successful).grid(row=3, column=0, columnspan=2)

2.3 统计页面

import tkinter.messagebox
import tkinter as tk  # 使用Tkinter前需要先导入
from database import *

def open():
    # 实例化object,建立窗口window
    window = tk.Toplevel()
    window.title('统计页面')
    window['bg']='#d0c0c0'
    window.geometry('300x500')

    data = []

    # 存放统计结果
    listbox = tk.Listbox(window, listvariable=data)
    # 日期输入框
    e_start = tk.Entry(window, show=None, font=('Arial', 14))
    e_start.insert(0,"起始日期")
    e_end = tk.Entry(window, show=None, font=('Arial', 14))  # 显示成明文形式
    e_end.insert(0,"终止日期")
    label_money = tk.Label(window, width=40, text="时间段内销售总额:空(请点击查询)")

    def get_sales():
        sales = db_get_sales()
        listbox.delete(0,"end")
        for sale in sales:
            listbox.insert("end",sale[0]+" 销售量"+str(sale[1]))

    def get_sales_time():
        start_date = e_start.get()
        end_date = e_end.get()
        try:
            datetime.datetime.strptime(start_date, '%Y-%m-%d')
            datetime.datetime.strptime(end_date, '%Y-%m-%d')
        except ValueError:
            tkinter.messagebox.showinfo(title='警告', message='日期不合法,范例2021-02-03')
            return
        sales = db_get_sales_time(start_date, end_date)
        listbox.delete(0,"end")
        if sales == ():
            tkinter.messagebox.showinfo(title='提示', message='该时间段没有销售')
        for sale in sales:
            listbox.insert("end",sale[0]+" 销售量"+str(sale[1]))

    def get_money_time():
        start_date = e_start.get()
        end_date = e_end.get()
        try:
            datetime.datetime.strptime(start_date, '%Y-%m-%d')
            datetime.datetime.strptime(end_date, '%Y-%m-%d')
        except ValueError:
            tkinter.messagebox.showinfo(title='警告', message='日期不合法,范例2021-02-03')
            return
        money = db_get_money_time(start_date, end_date)
        if money == None:
            tkinter.messagebox.showinfo(title='提示', message='该时间段没有销售')
            return
        label_money.config(text = "时间段内销售总额:"+str(money)+"元(点击第三个按钮刷新)")

    button_sales = tk.Button(window, text='按菜品销量排序(总)', width=20, height=2, command=get_sales)
    button_sales_time = tk.Button(window, text='按时间段销量排序', width=20, height=2, command=get_sales_time)
    button_sales_total = tk.Button(window, text='查询某时间段内销售总额', width=20, height=2, command=get_money_time)

    button_sales.pack()
    button_sales_time.pack()
    button_sales_total.pack()

    e_start.pack()
    e_end.pack()
    listbox.pack()
    label_money.pack()
    window.mainloop()

2.4 点餐系统

import tkinter.messagebox
import tkinter as tk  # 使用Tkinter前需要先导入

import discount
from database import *

food = {}
bills = []
# # bill_id = 0
# member_id = 3
# # server_id = 7
# member_cent = 0
# server_id = 0
food = db_get_all_food()


def open_order_table(table_id,bill_id,member_id):
    print("member_id:"+str(member_id))
    member_point = db_get_member_point(member_id)
    server_id = db_get_server_id(table_id)
    db_get_server_id(table_id)
    # 第1步,实例化object,建立窗口window
    window = tk.Toplevel()
    window['bg']='#d0c0c0'
    window.title('点餐系统')

    window.geometry('300x800')
    #在图形界面上创建一个标签label用以显示并放置
    var = tk.StringVar()  # 定义一个var用来将radiobutton的值和Label的值联系在一起.
    var.set("川菜")
    label_server = tk.Label(window, bg='#b8b0b0', width=20, text=str(server_id)+"号服务员为您服务")
    label_server.pack()
    label_food = tk.Label(window, bg='#b8b0b0', width=20, text="川菜")
    label_food.pack()
    label_order = tk.Label(window, bg='#b8b0b0', width=20, text="订单 " + "0元")
    # 对应菜品的显示
    listbox = tk.Listbox(window, listvariable=food["川菜"])
    for food_item in food[var.get()]:
        listbox.insert("end", food_item)
    listbox_bill = tk.Listbox(window, listvariable=bills)

    # 定义选项触发函数功能
    def print_category():
        #print(var.get())
        label_food.config(text=var.get())
        listbox.delete(0, "end")
        for food_item in food[var.get()]:
            listbox.insert("end", food_item)

    def add_bill(food_item):
        bills.append(food_item)
        bill_money=db_add_bill(bill_id,food_item.split(' ')[0])
        listbox_bill.insert("end", food_item)
        label_order.config(text="订单 " + str(bill_money) + "元")

    def submit_bill():

        listbox_bill.delete(0, "end")
        item_num = 0
        label_order.config(text="订单 " + str(item_num) + "元")
        db_submit_bill(member_id,bill_id)
        window.destroy()
        discount.open(member_point,member_id)

    def submit_food():
        if listbox.curselection() == ():
            tkinter.messagebox.showinfo(title='警告', message='请点击框内菜品才添加')
            return
        print(listbox.curselection())
        food_item = listbox.get(listbox.curselection())
        add_bill(food_item)

        # 创建三个radiobutton选项,其中variable=var, value='A'的意思就是,当我们鼠标选中了其中一个选项,把value的值A放到变量var中,然后赋值给variable
    for category in food:
        radiobutton = tk.Radiobutton(window, text=category, variable=var, value=category, command=print_category)
        radiobutton.pack()

    listbox.pack()
    button_submit = tk.Button(window, text='添加菜品', width=15, height=2, command=submit_food)
    button_submit.pack()

    label_order.pack()
    listbox_bill.pack()
    button_pay = tk.Button(window, text='结账', width=15, height=2, command=submit_bill)
    button_pay.pack()

    label_member_id = tk.Label(window, width=20, text="会员号:"+str(member_id))
    label_member_id.pack()
    label_server_id = tk.Label(window, width=20, text="服务员:" + str(server_id))
    label_server_id.pack()
    label_cent = tk.Label(window, width=20, text="积分:" + str(member_point))
    label_cent.pack()
    window.mainloop()

2.5 后台页面


import database_manage
from tkinter import messagebox
#import main
import tkinter as tk  # 使用Tkinter前需要先导入
from database_manage import *
from tkinter import *

def show():

    window = tk.Toplevel()
    window['bg'] = '#d0c0c0'
    window.title('后台页面')
    window.geometry('250x250')

    def inquire_menu():
        window_menu = tk.Toplevel()
        window_menu['bg'] = '#d0c0c0'
        window_menu.title('所有菜品页面')
        window_menu.geometry('220x230')

        food = {}
        food=database_manage.db_get_food()
        var = tk.StringVar()
        listbox = tk.Listbox(window_menu, listvariable=food)
        #listbox.Text(window,wigth=100,height=300)
        listbox.grid(row=0, column=6,ipadx=30,ipady=10,columnspan=5,rowspan=5)
        listbox.insert("end", "id:  "+" 类别: "+" 名称:"+" 价格:")
        for food_item in food:
            #listbox.insert("end", food[food_item][0])
            listbox.insert("end", food[food_item][0]+"    "+food[food_item][1]+"    "+food[food_item][2]+"    "+food[food_item][3])


    def add_menu():
        window_add = tk.Toplevel()
        window_add['bg'] = '#d0c0c0'
        window_add.title('添加菜品页面')
        window_add.geometry('300x200')

        Label(window_add, text='id_food').grid(row=1, column=0)
        id = Entry(window_add)
        id.grid(row=1, column=1)
        Label(window_add, text='category').grid(row=2, column=0)
        category = Entry(window_add)
        category.grid(row=2, column=1)

        Label(window_add, text='name').grid(row=3, column=0)
        name = Entry(window_add)
        name.grid(row=3, column=1)
        Label(window_add, text='price').grid(row=4, column=0)
        price = Entry(window_add)
        price.grid(row=4, column=1)



        def add():
            falg=db_get_all_categories(category)
            if(falg==1):
                ret=db_get_add(id,category,name,price)
                if(ret==1):
                    messagebox.showinfo(title='successful', message='添加成功')
                else:
                    messagebox.showinfo(title='失败', message='由于food表的外键约束,不能在pycharm里用语句添加')
            else:
                messagebox.showinfo(title='失败', message='category错误')

        Button(window_add, text='添加', command=add).grid(row=6, column=2, columnspan=2)


    def alter_menu():
        window_alter = tk.Toplevel()
        window_alter['bg'] = '#d0c0c0'
        window_alter.title('修改菜品页面')
        window_alter.geometry('300x200')

        Label(window_alter, text='菜品名称').grid(row=1, column=0)
        name = Entry(window_alter)
        name.grid(row=1, column=1)
        Label(window_alter, text='菜品价格').grid(row=2, column=0)
        price = Entry(window_alter)
        price.grid(row=2, column=1)


        def alters():
            falg = db_alter(name,price)
            if falg == 1:
                messagebox.showinfo(title='successful', message='修改成功')
            else:
                messagebox.showinfo(title='失败', message='修改失败')

        Button(window_alter, text='修改', command=alters).grid(row=6, column=2, columnspan=2)


    def delete_menu():
        window_delete = tk.Toplevel()
        window_delete['bg'] = '#d0c0c0'
        window_delete.title('删除菜品页面')
        window_delete.geometry('300x200')

        Label(window_delete, text='菜品名称').grid(row=1, column=0)
        name = Entry(window_delete)
        name.grid(row=1, column=1)

        def deletes():
            falg = db_delete(name)

            if falg == 1:
                messagebox.showinfo(title='successful', message='删除成功')
            else:
                messagebox.showinfo(title='失败', message='由于food表的外键约束,不能在pycharm里用语句删除')

        Button(window_delete, text='删除', command=deletes).grid(row=6, column=2, columnspan=2)

    def add_member():
        window_addm = tk.Toplevel()
        window_addm['bg'] = '#d0c0c0'
        window_addm.title('增加会员页面')
        window_addm.geometry('300x200')

        Label(window_addm, text='id_member').grid(row=0, column=0)
        member = Entry(window_addm)
        member.grid(row=0, column=1)
        Label(window_addm, text='name').grid(row=1, column=0)
        name = Entry(window_addm)
        name.grid(row=1, column=1)
        Label(window_addm, text='sex').grid(row=2, column=0)
        sex = Entry(window_addm)
        sex.grid(row=2, column=1)
        Label(window_addm, text='phone').grid(row=3, column=0)
        phone = Entry(window_addm)
        phone.grid(row=3, column=1)

        def adds():
            falg = db_add_member(member,name,sex,phone)
            if falg == 1:
                messagebox.showinfo(title='successful', message='增加成功')
            else:
                messagebox.showinfo(title='失败', message='增加失败')

        Button(window_addm, text='增加', command=adds).grid(row=6, column=2, columnspan=2)

    tk.Button(window, text='查询所有菜品', width=15, height=2,command=inquire_menu).grid(row=0, column=1)
    tk.Button(window, text='添加菜品', width=15, height=2,command=add_menu).grid(row=1, column=1)
    tk.Button(window, text='修改菜品价格', width=15, height=2,command=alter_menu).grid(row=2, column=1)
    tk.Button(window, text='删除菜品', width=15, height=2,command=delete_menu).grid(row=3, column=1)
    tk.Button(window, text='增加会员', width=15, height=2, command=add_member).grid(row=4, column=1)
    
    window.mainloop()

2.6 统计系统

import tkinter.messagebox
import tkinter as tk  # 使用Tkinter前需要先导入
from database import *

def open(points,member_id):
    # 第1步,实例化object,建立窗口window
    window = tk.Toplevel()
    # 第2步,给窗口的可视化起名字
    window.title('统计系统')
    # 第3步,设定窗口的大小(长 * 宽)
    window.geometry('300x500')
    window['bg']='#d0c0c0'
    # 优惠
    label_discount = tk.Label(window, bg='#b8b0b0', width=30, text ="选择优惠")
    label_discount.pack()

    # 存放统计结果
    discount = db_get_discountlist()
    print(discount)
    listbox = tk.Listbox(window, listvariable=discount)
    for discount_item in discount:
        if points < discount[discount_item][1]: # 积分不够规则所需
            continue
        off_price = str(discount[discount_item][0])
        require_points = str(discount[discount_item][1])
        listbox.insert("end","花费"+require_points+"积分获得"+off_price+"元优惠" )
    listbox.pack()

    def commit_discount():
        if listbox.curselection() == ():
            tkinter.messagebox.showinfo(title='警告', message='请点击框内优惠才提交')
            return
        db_commit_discount(discount[listbox.curselection()[0]+1][1],member_id)
        cancle()

    def cancle():
        window.destroy()
        tkinter.messagebox.showinfo(title='结账', message='结账成功,欢迎下次再来!')

    # 确认优惠
    button_commit = tk.Button(window, text='使用', width=15, height=2, command=commit_discount)
    button_commit.pack()
    button_cancel = tk.Button(window, text='不使用', width=15, height=2, command=cancle)
    button_cancel.pack()

    window.mainloop()

2.7 登录页面连接数据库


import pymysql


def db_get_user111(name,passwd):
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """select user_id,user_password from user"""
        entry1 = name.get()

        entry2 = passwd.get()

        cursor.execute(sql)
        results = cursor.fetchall()

        for row in results:
            uid=row[0]
            pwd=row[1]
            if entry1==uid and entry2==pwd:
                db.close()
                return 1
        return 0
    except:
        db.rollback()
        db.close()
        return 0

def db_get_food():
    db = pymysql.connect(host='localhost', user='root', password='123456', db="restaurant", charset="utf8")
    cursor = db.cursor()
    food = {}
    sql = """select id_food,category,name_food,price from food"""
    try:
        # 执行sql
        cursor.execute(sql)
        # 处理结果集
        results = cursor.fetchall()

        for row in results:
            food[str(row[0])]=[str(row[0]),row[1],row[2],str(row[3])]
        db.close()
        return food
    except Exception as e:
        # print(e)
        print('查询所有数据失败')
        db.rollback()
        db.close()
        return 0


def db_get_all_categories(category):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    sql = """select category from categories"""
    try:
        category = category.get()
        cursor.execute(sql)
        results = cursor.fetchall()

        for row in results:
            if category == row[0]:
                return 1
        return 0
    except:
        print("wrong:db_get_all_categories")
        db.rollback()
        db.close()
        return 0


def db_get_add(id,category,name,price):
    db = pymysql.connect(host='localhost', user='root', password='123456', db="restaurant", charset="utf8")
    cursor = db.cursor()


    try:

        sql = """insert into food(id_food,category,name_food,introduction,price,url) values(%s,%s,%s,%s,%s,%s)"""
        value = (id, category, name, 'null', price, 'NULL')
        # 执行sql
        cursor.execute(sql,value)
        db.commit()


        db.close()
        return 1
    except Exception as e:
        print(e)
        db.rollback()
        db.close()
        return 0


def db_alter(name,price):
    db = pymysql.connect(host='localhost', user='root', password='123456', db="restaurant", charset="utf8")
    cursor = db.cursor()
    try:
        price = price.get()
        name = name.get()
        sql = """update food set price = %s where name_food = %s"""
        value = ( price , name )
        # 执行sql
        cursor.execute(sql,value)
        db.commit()

        db.close()
        return 1
    except Exception as e:
        print(e)
        db.rollback()
        db.close()
        return 0


def db_delete(name):
    db = pymysql.connect(host='localhost', user='root', password='123456', db="restaurant", charset="utf8")
    cursor = db.cursor()
    try:
        name = name.get()
        sql = """delete from food where name_food=%s"""
        #value = (name)
        # 执行sql
        cursor.execute(sql, name)
        db.commit()

        db.close()
        return 1
    except Exception as e:
        print(e)
        db.rollback()
        db.close()
        return 0

def db_add_member(member,name,sex,phone):
    db = pymysql.connect(host='localhost', user='root', password='123456', db="restaurant", charset="utf8")
    cursor = db.cursor()
    try:
        member=member.get()
        name = name.get()
        sex=sex.get()
        phone=phone.get()
        sql = """insert into member(id_member,name_member,points,sex,phone_num) values(%s,%s,0,%s,%s) """
        value = (member,name,sex,phone)
        # 执行sql
        cursor.execute(sql, value)
        db.commit()

        db.close()
        return 1
    except Exception as e:
        print(e)
        db.rollback()
        db.close()
        return 0

2.8 其他页面连接数据库

在这里插入代码片import datetime
#import reig_manage
import pymysql

def db_get_table():
    # 打开数据库连接,创建一个数据库对象
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    sql = """select id_table, id_server, id_bill
                from tables"""
    try:

        tables={}
        cursor.execute(sql) # 执行sql语句
        results = cursor.fetchall()      #获取所有数据
        for row in results:
            print(row)
            occupied = row[2]
            if occupied:
               occupied = "占用"
            else:
                occupied = "空闲"
            server = row[1]
            tables["餐桌"+str(row[0])]=[occupied,server]
        db.close()
        return tables
    except:

        print("wrong:get_table")
        db.rollback()
        db.close()
        return {}


# 找到下一个bill的id返回,并且将其设置成当前选择的table的bill,表示入座
def db_sit(table_num):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    sql = """select max(id_bill)
                    from bills"""

    try:

        max = 0
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            max = row[0]
        sql2 = """insert into bills(id_bill,id_table,id_member,time_order,money)
                    values(%d,%d,NULL,"%s",0)"""% \
               (max+1,table_num,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        cursor.execute(sql2)
        sql3 = """update tables
                set id_bill=%d
                where id_table = %d
                """ % \
               (max+1,table_num)
        cursor.execute(sql3)
        db.commit()   #插入数据
        db.close()
        return max+1
    except:

        print("wrong:db_sit")
        db.rollback()
        db.close()
        return 0


def db_get_server_id(table_id):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    sql = """select id_server
                    from tables
                    where id_table = %d"""%(table_id)
    print(sql)
    try:

        server_id = 0
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            server_id = row[0]
        db.close()
        return server_id
    except:

        print("wrong:db_get_server_id")
        db.rollback()
        db.close()
        return 0


def db_get_all_food():
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    food = {}
    sql = """select category from categories"""
    print(sql)
    try:

        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            food[row[0]]=[]
        sql2 = """select category, name_food, price
                    from food
                """
        cursor.execute(sql2)
        results = cursor.fetchall()
        for row in results:
            food[row[0]].append(row[1]+" "+str(row[2])+"元")
        db.close()
        return food
    except:

        print("wrong:db_get_all_food")
        db.rollback()
        db.close()
        return 0


def db_add_bill(bill_id,food_name):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    food = {}
    try:
        current_money = 0

        sql_find_food_id = """select id_food, price 
                    from food
                    where name_food = "%s" """ %(food_name)
        cursor.execute(sql_find_food_id)
        results = cursor.fetchall()
        for row in results:
            id = row[0]
            price = row[1]
        sql_findexistsfood = """select *
                    from bill_food
                    where id_food = %d and id_bill = %d 
                    """%(id,bill_id)
        cursor.execute(sql_findexistsfood)
        if cursor.fetchall()==():
            sql2 = """insert into bill_food
                        values(%d,%d,1)
                        """%(id,bill_id)
        else:
            sql2 = """update bill_food
                        set num =num +1
                    where id_food = %d and id_bill = %d 
                                    """ % (id, bill_id)
        cursor.execute(sql2)
        sql3 = """update bills
                    set money = money+%d 
                    where id_bill = %d
                            """ % (price, bill_id)
        cursor.execute(sql3)
        db.commit()
        sql4 = """select money
                    from bills
                    where id_bill = %d
                            """ % (bill_id)
        cursor.execute(sql4)
        results = cursor.fetchall()
        for row in results:
            current_money = row[0]
            print(current_money)
            db.close()
            return current_money
    except:

        print("wrong:db_add_bill")
        db.rollback()
        db.close()
        return 0


def db_submit_bill(member_id,id_bill):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """update tables
                set id_bill = NULL
                where id_bill = %d"""%(id_bill)
        cursor.execute(sql)
        sql2 = """update bills
                set time_pay = "%s"
                where id_bill = %d"""%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),id_bill)
        cursor.execute(sql2)
        if member_id != None:
            sql3 = """select money
                        from bills
                        where id_bill = %d
                                """ % (id_bill)
            cursor.execute(sql3)
            results = cursor.fetchall()
            for row in results:
                current_money = row[0]
            sql4 = """update member
                        set points = points+ %s
                        where id_member = %d""" % (current_money,member_id)
            cursor.execute(sql4)
        db.commit()
        db.close()
    except:

        print("wrong:db_submit_bill")
        db.rollback()
        db.close()


def db_get_sales():
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """SELECT name_food, sum(num)
                FROM bill_food natural join food
                group by id_food
                order by sum(num) desc"""
        cursor.execute(sql)
        results = cursor.fetchall()
        db.close()
        return results
    except:

        print("wrong:db_get_sales")
        db.rollback()
        db.close()
        return ()


def db_get_sales_time(start_time, end_time):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """SELECT name_food, sum(num)
                FROM bill_food natural join bills natural join food
                where time_pay between "%s 00:00:00" and "%s 00:00:00"
                group by id_food
                order by sum(num) desc;"""%(start_time,end_time)
        cursor.execute(sql)
        results = cursor.fetchall()
        db.close()
        return results
    except:

        print("wrong:db_get_sales_time")
        db.rollback()
        db.close()
        return ()


def db_get_money_time(start_time, end_time):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """SELECT sum(money)
                FROM bills
                    where time_pay between "%s 00:00:00" and "%s 00:00:00"
                    """ % (start_time, end_time)
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            return row[0]
        db.close()
        return 0
    except:

        print("wrong:db_get_money_time")
        db.rollback()
        db.close()
        return 0

def db_clear_table():
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:

        sql = """update tables
                set id_bill = NULL
                where id_table <> 100"""
        cursor.execute(sql)
        db.commit()
        db.close()
    except:

        print("wrong:db_clear_table")
        db.rollback()
        db.close()


def db_get_member_point(member_id):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    try:
        if member_id == None:
            return 0

        sql = """select points
                    from member
                    where id_member = %s"""%(member_id)
        cursor.execute(sql)
        results = cursor.fetchall()
        if results == ():
            sql2 = """insert into member
                    values(%s,null,0,null,null)""" % (member_id)
            cursor.execute(sql2)
            db.commit()
            db.close()
            return 0
        db.close()
        for row in results:
            return row[0]
    except:

        print("wrong:db_ensure_member_id")
        db.rollback()
        db.close()

def db_get_discountlist():
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    discount = {}
    sql = """select * from discounts"""
    print(sql)
    try:

        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            discount[row[0]] = [row[1],row[2]]
        db.close()
        return discount
    except:

        print("wrong:db_get_discountlist")
        db.rollback()
        db.close()
        return 0

def db_commit_discount(points,member_id):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", password="123456", db="restaurant", charset="utf8")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    discount = {}
    sql = """update member
            set points = points - %s
            where id_member = %s"""%(points,member_id)
    try:
        cursor.execute(sql)
        db.commit()
        db.close()
    except:
        print("wrong:db_commit_discount")
        db.rollback()
        db.close()
        return 0

四、源码获取

餐厅点餐系统

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/15041.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

最常用的从A到Z的Linux命令,真的很好记,三分钟刷完!

Linux的命令行是一个非常强大的工具。如果你知道如何利用Linux命令&#xff0c;你可以轻松地在Linux系统中执行各种任务。在这篇文章中&#xff0c;我们将介绍从A到Z的Linux命令。 alias alias命令允许你为常用的命令设置一个短的别名&#xff0c;以节省时间和减少敲击。例如&…

力扣,合并石头最低成本算法题

1&#xff1a;这个题有题解&#xff0c;自己可以去看力扣&#xff0c;合并石头 2&#xff1a;网上也有视频自己去看视频讲解 3&#xff1a;下面我自己的一些理解 4&#xff1a;原需求&#xff1a; 5&#xff1a;代码&#xff1a;使用贪心算法和最小堆来求解&#xff1a; im…

第九章 子查询

文章目录 前言一、.需求分析与问题解决1 、实际问题2 、子查询的基本使用3 、子查询的分类 二、单行子查询1、单行比较操作符2、代码示例3、 HAVING 中的子查询4、CASE中的子查询5、 子查询中的空值问题6、非法使用子查询 三、多行子查询1、 多行比较操作符2、代码示例3 、空值…

这可能是你看过最详细的Java集合篇【二】—— LinkedList

文章目录 LinkedList继承关系数据结构变量构造方法添加元素相关方法查找元素相关方法删除元素相关方法清空方法遍历方法其它方法常见面试题 LinkedList LinkedList底层数据结构是双向链表。链表数据结构的特点是每个元素分配的空间不必连续、插入和删除元素时速度非常快、但访…

python@可变对象和不可变对象@按值传递和引用传递@python运行可视化工具

文章目录 可变对象和不可变对象&#x1f388;可视化工具&#x1f388;可变对象和idegeg变量名和内存地址&#x1f388;函数调用对参数的修改&#x1f602;Note 按值传递vs引用传递note&#x1f388;如何借助函数修改外部变量的值?Note 可变对象和不可变对象&#x1f388; 在Py…

数据库的概念?怎么在linux内安装数据库?怎么使用?

目录 一、概念 二、mysql安装及设置 1.安装mysql 2.数据库服务启动停止 三、数据库基本操作 1、数据库的登录及退出 2、数据表的操作 3、mysql查询操作 一、概念 数据库:是存放数据的仓库&#xff0c;它是一个按数据结构来存储和管理数据的计算机软件系统。数据库管理…

SQLServer的内存管理架构

内存管理架构说明 一、Windows的虚拟内存管理器二、SQL Server 内存体系结构2.1、传统&#xff08;虚拟&#xff09;内存2.2、地址窗口扩展 &#xff08;AWE&#xff09; 内存 三、从 SQL Server 2012 &#xff08;11.x&#xff09; 开始发生的改变3.1、对内存管理的更改3.2、对…

安装多个NodeJS windows上安装多个Nodejs版本 解决vue2/vue3同时运行

第一步下载nvm-windowsnvm-windows 下载地址&#xff1a;Github最新下载地址 进入之后直接下载 第二步 安装NVM 注意路径一定不要包含空格 中文否则会报错 点击安装之后 如果之前安装了nodejs的话会提示 希望nvm管理已安装node 版本吗 点击 是 即可 安装完成后 打开 cmd 输入 n…

Bito:一款 iead/webstorm 神级插件,由 ChatGPT 团队开发,堪称辅助神器

前言&#xff1a; idea(后端)&#xff0c;webstorm(前端)中可以用的一款辅助插件&#xff1a;Bito 个人尝试体验效果&#xff1a; 优点是&#xff1a;可以自动完成一些场景代码。 缺点&#xff1a;太慢了&#xff0c;大部分时间一直转圈 摘取文档&#xff1a; 什么是Bito&…

TDA4VM/VH 芯片硬件 mailbox

请从官网下载 TD4VM 技术参考手册&#xff0c;地址如下&#xff1a; TDA4VM 技术参考手册地址 概述 (Mailbox 的介绍在 TRM 的第7.1章节) Mailbox 使用邮箱中断机制实现了 VM 芯片的核间通信。 Mailbox 是集成在 NAVSS0 域下的一个外设&#xff08;NAVSS0 的说明可以查看&a…

flink on k8s提交任务

目录 相关文档前置准备构建镜像提交任务 相关文档 https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/deployment/resource-providers/native_kubernetes/ 前置准备 flink的lib目录下放入两个依赖 bcpkix-jdk15on-1.68.jar bcprov-jdk15on-1.69.jar 创建用户…

CRM客户关系管理系统主要有哪些功能?

一、CRM客户管理系统是什么 客户关系管理&#xff08;Customer Relationship Management&#xff0c;简称CRM&#xff09;&#xff0c;是指企业为提高核心竞争力&#xff0c;利用相应的信息技术以及互联网技术协调企业与顾客间在销售、营销和服务上的交互&#xff0c;从而提升…

关于HTML5画布canvas的功能

一、画布的使用 1、首先创建一个画布&#xff08;canvas&#xff09; <canvas id”myCanvas” width”200” height”100” style”border:1px solid #000000”></canvas> 2、使用JavaScript来绘制图像 <script> Var cdocument.getElementByID(“myCanv…

AlgoC++第八课:手写BP

目录 手写BP前言1. 数据加载2. 前向传播3. 反向传播总结 手写BP 前言 手写AI推出的全新面向AI算法的C课程 Algo C&#xff0c;链接。记录下个人学习笔记&#xff0c;仅供自己参考。 本次课程主要是手写 BP 代码 课程大纲可看下面的思维导图 1. 数据加载 我们首先来实现下MNIST…

【别再困扰于LeetCode接雨水问题了 | 从暴力法=>动态规划=>单调栈】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

用 ChatGPT 进行阅读理解题目的问答

阅读理解出题 阅读理解题是语言学习过程中一种重要的练习方式。无论语文还是英语考试中&#xff0c;阅读理解题都占有相当大的分值。ChatGPT 作为一种大语言模型&#xff0c;在处理自然语言理解任务中具有很大的优势。广大教师和学生家长们&#xff0c;都可以尝试用 ChatGPT 进…

springboot使用mybatis

扫描mapper接口的位置&#xff0c;生成代理对象 在application.properties配置数据源 测试: 在application.properties配置mybaits&#xff0c;支持驼峰命名&#xff0c;下划线 结果映射: Insert语句例子 在application.properties配置日志 更新 总结: 结果复用 ResultMap第二种…

Oracle-12c版本之后替换OCR磁盘组步骤

背景: 用户有一套Oracle12.2的RAC集群&#xff0c;在安装配置的时候&#xff0c;OCR磁盘只使用了单块磁盘external的模式&#xff0c;想替换成包含三块磁盘组成员normal模式的磁盘组 OCR磁盘组存储的对象: 在替换OCR磁盘之前&#xff0c;我们先确认需要迁移的OCR磁盘组存储的对…

五分钟学会在微信小程序中使用 vantUI 组件库

前言 我们在开发微信小程序时&#xff0c;设计和实现好用的用户界面无疑是至关重要的一步。但是微信小程序官方自带的 UI 组件库无法满足很多使用场景&#xff0c;这个时候就需要我们使用一些第三方的 UI 组件库。而 vant Weapp 作为一款优秀的前端 UI 组件库&#xff0c;可以帮…

【软件测试】项目测试—MySQL数据库操作应用场景?必会知识详全(超详细)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 数据库在软件测试…