帮我用Python写一个程序,使用 mysql 保存数据,实现超市日常管理中以下业务。" 一、建立以下数据表并填充数据+登录+' 用户表: users _学号后四位+' 列名: id _学号后四位用户名 name 学号后四位密码: pwd _学号后四位*产品表: products _学号后四位. 列名: id _学号后四位编号 num 学号后四位名称 name 学号后四位单价 price 学号后四位折扣 discount _学号后四位 订单表: orders _学号后四位. 列名: id _学号后四位编号 num 学号后四位货品数量 count 学号后四位金额 arnount 学号后四位. 二、实现以下菜单并实现业务0用户登录 1查看所有产品信息。"2查找商品(用户输入商品名称,查看价格和折扣):- 3添加商品(需要程序生成编号),- 4修改商品价格和折扣(折扣为0.1-0.99之间):. 5删除商品(根据编号): 6查看所有订单:+ 7.订单统计(显示总销售额和货品数量): 8.商品结算。、
时间: 2024-02-01 09:16:33 浏览: 73
好的,以下是使用Python和MySQL实现超市日常管理的程序:
1. 首先需要安装MySQL的Python库pymysql:
```python
pip install pymysql
```
2. 建立数据库和数据表,并填充数据:
```python
import pymysql
# 连接数据库
db = pymysql.connect(host='localhost', user='root', password='password')
cursor = db.cursor()
# 创建数据库
cursor.execute('CREATE DATABASE IF NOT EXISTS supermarket')
# 使用数据库
cursor.execute('USE supermarket')
# 创建用户表
cursor.execute('CREATE TABLE IF NOT EXISTS users (id VARCHAR(10), name VARCHAR(20), pwd VARCHAR(20))')
cursor.execute("INSERT INTO users VALUES ('0001', 'user1', '1111')")
cursor.execute("INSERT INTO users VALUES ('0002', 'user2', '2222')")
cursor.execute("INSERT INTO users VALUES ('0003', 'user3', '3333')")
# 创建产品表
cursor.execute('CREATE TABLE IF NOT EXISTS products (id VARCHAR(10), num VARCHAR(10), name VARCHAR(20), price FLOAT, discount FLOAT)')
cursor.execute("INSERT INTO products VALUES ('0001', '0001', 'product1', 10, 0.9)")
cursor.execute("INSERT INTO products VALUES ('0002', '0002', 'product2', 20, 0.8)")
cursor.execute("INSERT INTO products VALUES ('0003', '0003', 'product3', 30, 0.7)")
# 创建订单表
cursor.execute('CREATE TABLE IF NOT EXISTS orders (id VARCHAR(10), num VARCHAR(10), count INT, amount FLOAT)')
cursor.execute("INSERT INTO orders VALUES ('0001', '0001', 2, 18)")
cursor.execute("INSERT INTO orders VALUES ('0002', '0002', 3, 48)")
cursor.execute("INSERT INTO orders VALUES ('0003', '0003', 1, 21)")
db.commit()
```
3. 实现用户登录功能:
```python
def login():
name = input('请输入用户名:')
pwd = input('请输入密码:')
cursor.execute("SELECT * FROM users WHERE name=%s AND pwd=%s", (name, pwd))
if cursor.fetchone():
print('登录成功!')
else:
print('用户名或密码错误!')
```
4. 实现查看所有产品信息功能:
```python
def show_products():
cursor.execute('SELECT * FROM products')
products = cursor.fetchall()
for product in products:
print(product)
```
5. 实现查找商品功能:
```python
def search_product():
name = input('请输入要查找的商品名称:')
cursor.execute("SELECT price, discount FROM products WHERE name=%s", name)
result = cursor.fetchone()
if result:
price, discount = result
print(f'商品名称:{name},单价:{price},折扣:{discount}')
else:
print('找不到该商品!')
```
6. 实现添加商品功能:
```python
import random
def add_product():
name = input('请输入商品名称:')
price = float(input('请输入商品单价:'))
discount = float(input('请输入商品折扣(0.1-0.99):'))
num = str(random.randint(1000, 9999))
id = '0001' # 假设当前用户学号后四位为0001
cursor.execute("INSERT INTO products VALUES (%s, %s, %s, %s, %s)", (id, num, name, price, discount))
db.commit()
print('添加成功!')
```
7. 实现修改商品价格和折扣功能:
```python
def modify_product():
num = input('请输入要修改的商品编号:')
price = float(input('请输入新的商品单价:'))
discount = float(input('请输入新的商品折扣(0.1-0.99):'))
cursor.execute("UPDATE products SET price=%s, discount=%s WHERE num=%s", (price, discount, num))
db.commit()
print('修改成功!')
```
8. 实现删除商品功能:
```python
def delete_product():
num = input('请输入要删除的商品编号:')
cursor.execute("DELETE FROM products WHERE num=%s", num)
db.commit()
print('删除成功!')
```
9. 实现查看所有订单功能:
```python
def show_orders():
cursor.execute('SELECT * FROM orders')
orders = cursor.fetchall()
for order in orders:
print(order)
```
10. 实现订单统计功能:
```python
def order_statistics():
cursor.execute('SELECT COUNT(*), SUM(count), SUM(amount) FROM orders')
result = cursor.fetchone()
count, total_count, total_amount = result
print(f'总销售额:{total_amount},货品数量:{total_count}')
```
11. 实现商品结算功能:
```python
def checkout():
num = input('请输入要结算的商品编号:')
cursor.execute("SELECT price, discount FROM products WHERE num=%s", num)
result = cursor.fetchone()
if result:
price, discount = result
count = int(input('请输入购买数量:'))
amount = price * discount * count
id = '0001' # 假设当前用户学号后四位为0001
cursor.execute("INSERT INTO orders VALUES (%s, %s, %s, %s)", (id, num, count, amount))
db.commit()
print(f'结算成功,应付金额:{amount}')
else:
print('找不到该商品!')
```
12. 最后将所有功能整合到一个主函数中:
```python
def main():
while True:
print('超市日常管理系统')
print('0. 用户登录')
print('1. 查看所有产品信息')
print('2. 查找商品')
print('3. 添加商品')
print('4. 修改商品价格和折扣')
print('5. 删除商品')
print('6. 查看所有订单')
print('7. 订单统计')
print('8. 商品结算')
choice = input('请选择功能:')
if choice == '0':
login()
elif choice == '1':
show_products()
elif choice == '2':
search_product()
elif choice == '3':
add_product()
elif choice == '4':
modify_product()
elif choice == '5':
delete_product()
elif choice == '6':
show_orders()
elif choice == '7':
order_statistics()
elif choice == '8':
checkout()
else:
print('无效的选择!')
if __name__ == '__main__':
main()
```
运行程序后,就可以根据菜单选择相应的功能进行操作了。
阅读全文