使用 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.商品结算。,每个声明的变量加上_0314结尾,并且所有自定义的变量加上_0314,例如a_0314=1,。然后还要教我用MYSQL建立这个的数据库表
时间: 2024-02-05 17:12:36 浏览: 260
javafxtableviewjdbc:Tableview 使用 Javafx 填充 mysql 数据库中的数据
好的,下面是建立这个数据库表的 SQL 语句:
```
CREATE DATABASE market_0314;
USE market_0314;
CREATE TABLE users (
id VARCHAR(10) NOT NULL,
name VARCHAR(20) NOT NULL,
pwd VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE products (
id VARCHAR(10) NOT NULL,
num VARCHAR(10) NOT NULL,
name VARCHAR(50) NOT NULL,
price INT NOT NULL,
discount FLOAT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE orders (
id VARCHAR(10) NOT NULL,
num VARCHAR(10) NOT NULL,
count INT NOT NULL,
amount INT NOT NULL,
PRIMARY KEY (id)
);
```
然后我们添加一些测试数据:
```
INSERT INTO users VALUES ('0001', 'user1_0314', 'pwd1_0314');
INSERT INTO users VALUES ('0002', 'user2_0314', 'pwd2_0314');
INSERT INTO users VALUES ('0003', 'user3_0314', 'pwd3_0314');
INSERT INTO products VALUES ('0001', '001', 'product1_0314', 100, 0.9);
INSERT INTO products VALUES ('0002', '002', 'product2_0314', 200, 0.8);
INSERT INTO products VALUES ('0003', '003', 'product3_0314', 300, 0.7);
INSERT INTO orders VALUES ('0001', '001', 10, 900);
INSERT INTO orders VALUES ('0002', '002', 5, 800);
INSERT INTO orders VALUES ('0003', '003', 2, 420);
```
现在我们来实现菜单:
```
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="market_0314"
)
# 登录
def login():
id_0314 = input("请输入学号后四位:")
pwd_0314 = input("请输入密码:")
cursor = mydb.cursor()
sql = "SELECT * FROM users WHERE id=%s AND pwd=%s"
val = (id_0314, pwd_0314)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
print("登录成功!")
return id_0314
else:
print("登录失败!")
return None
# 查看所有产品信息
def show_products():
cursor = mydb.cursor()
sql = "SELECT * FROM products"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row[1], row[2], row[3], row[4])
# 查找商品
def find_product():
name_0314 = input("请输入商品名称:")
cursor = mydb.cursor()
sql = "SELECT * FROM products WHERE name=%s"
val = (name_0314,)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
print(result[2], result[3], result[4])
else:
print("未找到该商品!")
# 添加商品
def add_product():
num_0314 = input("请输入商品编号:")
name_0314 = input("请输入商品名称:")
price_0314 = int(input("请输入商品单价:"))
discount_0314 = float(input("请输入商品折扣(0.1-0.99):"))
cursor = mydb.cursor()
sql = "INSERT INTO products (id, num, name, price, discount) VALUES (%s, %s, %s, %s, %s)"
val = (num_0314 + '_0314', num_0314, name_0314, price_0314, discount_0314)
cursor.execute(sql, val)
mydb.commit()
print("商品添加成功!")
# 修改商品价格和折扣
def update_product():
id_0314 = input("请输入商品编号:")
price_0314 = int(input("请输入商品单价:"))
discount_0314 = float(input("请输入商品折扣(0.1-0.99):"))
cursor = mydb.cursor()
sql = "UPDATE products SET price=%s, discount=%s WHERE num=%s"
val = (price_0314, discount_0314, id_0314)
cursor.execute(sql, val)
mydb.commit()
print("商品信息修改成功!")
# 删除商品
def delete_product():
id_0314 = input("请输入商品编号:")
cursor = mydb.cursor()
sql = "DELETE FROM products WHERE num=%s"
val = (id_0314,)
cursor.execute(sql, val)
mydb.commit()
print("商品删除成功!")
# 查看所有订单
def show_orders():
cursor = mydb.cursor()
sql = "SELECT * FROM orders"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row[1], row[2], row[3])
# 订单统计
def order_stats():
cursor = mydb.cursor()
sql = "SELECT SUM(count), SUM(amount) FROM orders"
cursor.execute(sql)
result = cursor.fetchone()
print("总销售额:", result[1])
print("货品数量:", result[0])
# 商品结算
def checkout():
id_0314 = input("请输入商品编号:")
count_0314 = int(input("请输入商品数量:"))
cursor = mydb.cursor()
sql = "SELECT * FROM products WHERE num=%s"
val = (id_0314,)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
amount_0314 = int(result[3] * result[4] * count_0314)
sql = "INSERT INTO orders (id, num, count, amount) VALUES (%s, %s, %s, %s)"
val = (id_0314 + '_0314', id_0314, count_0314, amount_0314)
cursor.execute(sql, val)
mydb.commit()
print("结算成功!")
else:
print("未找到该商品!")
# 菜单
while True:
print("0. 用户登录")
print("1. 查看所有产品信息")
print("2. 查找商品")
print("3. 添加商品")
print("4. 修改商品价格和折扣")
print("5. 删除商品")
print("6. 查看所有订单")
print("7. 订单统计")
print("8. 商品结算")
choice_0314 = int(input("请选择功能:"))
if choice_0314 == 0:
user_id_0314 = login()
if user_id_0314:
print("欢迎," + user_id_0314)
elif choice_0314 == 1:
show_products()
elif choice_0314 == 2:
find_product()
elif choice_0314 == 3:
add_product()
elif choice_0314 == 4:
update_product()
elif choice_0314 == 5:
delete_product()
elif choice_0314 == 6:
show_orders()
elif choice_0314 == 7:
order_stats()
elif choice_0314 == 8:
checkout()
else:
print("无效的选择!")
阅读全文