DB Project ----- MySQL & Python Project

DB create

在这里插入图片描述

  • There are many shops in an online retail website. Each shop has a shop id, shop
    name, rating, location, and a list of items on sale in the shop. Each item has an
    item id, name, price, and at most 3 keywords to describe the item, which depend
    on the shop.

  • For every customer, we must capture customer id, telephone number, and address.

  • Each order is made by a customer and must contain at least an item of a shop. An
    order can contain items of different shops.

目前有一个具有以下表结构的MySQL数据库:

  1. shops 表:
    shop_id (主键)
    shop_name
    rating
    location
  2. items 表:
    item_id (主键)
    name
    price
    keyword1
    keyword2
    keyword3
    shop_id (外键)
  3. customers 表:
    customer_id (主键)
    telephone_number
    address
  4. orders 表:
    order_id (主键)
    customer_id (外键)
    item_id (外键)

Note:
每个订单都必须至少包含一件商品,每个订单可以包含来自不同商店的商品。

Python Connect

使用Python中的MySQL连接器(如mysql-connector)来连接到MySQL数据库,并使用SQL语句来查询和操作数据库。

例如,要获取所有商店的名称和评级,您可以使用以下代码:

import mysql.connector

# 创建数据库连接
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="root",
  database="DB Project"
)

# 创建游标对象
mycursor = mydb.cursor()

# 执行查询
mycursor.execute("SELECT shop_name, rating FROM shops")

# 获取查询结果
results = mycursor.fetchall()

# 打印查询结果
for result in results:
  print(result)

Function achieve

在这里插入图片描述

order cancel

在这里插入图片描述

  • 添加一个新函数,用于实现订单取消的功能。
  • 该函数需要接受两个参数:订单ID和要取消的商品ID列表。
  • 如果要取消整个订单,商品ID列表应该为空。
def cancel_order(order_id, item_ids):
    """
    取消订单或取消订单中的部分商品
    :param order_id: 要取消的订单ID
    :param item_ids: 要取消的商品ID列表,如果要取消整个订单,应该传入空列表
    :return: 如果取消成功,返回True,否则返回False
    """
    order = get_order(order_id)
    if order is None:
        print("订单不存在")
        return False
    
    # 如果item_ids为空,则取消整个订单
    if not item_ids:
        order["status"] = "canceled"
        update_order(order)
        return True
    
    # 取消部分商品
    items = order["items"]
    for item_id in item_ids:
        for i in range(len(items)):
            if items[i]["id"] == item_id:
                del items[i]
                update_order(order)
                return True
    
    print("商品不存在")
    return False

在OrderWindow类中添加一个新按钮和相应的槽函数,用于调用刚刚添加的cancel_order函数。

class OrderWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        # ...
        
        # 取消订单按钮
        self.cancel_button = QtWidgets.QPushButton(self)
        self.cancel_button.setGeometry(QtCore.QRect(690, 470, 100, 30))
        self.cancel_button.setText("取消订单")
        self.cancel_button.clicked.connect(self.cancel_order)
        
        # ...
    
    def cancel_order(self):
        # 获取选中的行
        selected_row = self.order_table.currentRow()
        if selected_row == -1:
            QtWidgets.QMessageBox.warning(self, "警告", "请选择要取消的订单")
            return
        
        # 获取订单ID和选中的商品ID
        order_id = self.order_table.item(selected_row, 0).text()
        item_ids = []
        for item in self.order_table.selectedItems():
            if item.column() == 2:
                item_ids.append(int(item.text()))
        
        # 调用cancel_order函数取消订单或商品
        if function.cancel_order(order_id, item_ids):
            self.show_orders()
        else:
            QtWidgets.QMessageBox.warning(self, "警告", "取消订单失败")

Summary:

  1. 创建了一个名为cancel_button的新按钮,并将其放置在窗口底部。
  2. 创建了一个名为cancel_order的新槽函数,该函数将从选中的行和列中获取订单ID和要取消的商品ID,并将它们作为参数传递给cancel_order函数。如果cancel_order函数返回True,则更新订单列表。否则,显示一个错误消息框。

未完待续……

猜你喜欢

转载自blog.csdn.net/weixin_45646640/article/details/130104167
今日推荐