USE product_db; ALTER TABLE products ADD COLUMN remark TEXT COMMENT '商品备注信息,支持长文本' AFTER cost_price;
2. 修改数据访问层(product_dao.py)
需要在以下函数中添加remark字段的处理:
修改get_all_products函数:
python
def get_all_products(keyword=''): # ... 前面代码不变 ... if keyword: sql = """ SELECT id, code, model, name, image_path, DATE_FORMAT(shelf_date, '%%Y-%%m-%%d') as shelf_date, supplier, cost_price, remark, created_at FROM products WHERE code LIKE %s OR name LIKE %s OR supplier LIKE %s ORDER BY id DESC """ else: sql = """ SELECT id, code, model, name, image_path, DATE_FORMAT(shelf_date, '%%Y-%%m-%%d') as shelf_date, supplier, cost_price, remark, created_at FROM products ORDER BY id DESC """ # ... 后面代码不变 ...
修改get_product_by_id函数:
python
def get_product_by_id(product_id): # ... 代码 ... sql = """ SELECT id, code, model, name, image_path, DATE_FORMAT(shelf_date, '%%Y-%%m-%%d') as shelf_date, supplier, cost_price, remark, created_at FROM products WHERE id = %s """ # ... 后面代码不变 ...
修改create_product函数:
python
def create_product(code, model, name, image_path, shelf_date, supplier, cost_price, remark=None): """ 新增商品记录 """ conn = get_connection() if not conn: return False, "数据库连接失败" try: cursor = conn.cursor() # 插入SQL语句(添加 remark 字段) sql = """ INSERT INTO products (code, model, name, image_path, shelf_date, supplier, cost_price, remark) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """ cursor.execute(sql, (code, model, name, image_path, shelf_date, supplier, cost_price, remark)) conn.commit() new_id = cursor.lastrowid return True, new_id except Exception as e: conn.rollback() return False, str(e) finally: if 'cursor' in locals(): cursor.close() if conn: conn.close()
修改update_product函数:
python
def update_product(product_id, code, model, name, image_path, shelf_date, supplier, cost_price, remark=None): """ 更新商品信息 """ conn = get_connection() if not conn: return False, "数据库连接失败" try: cursor = conn.cursor() if image_path: # 情况1:同时更新图片路径和备注 sql = """ UPDATE products SET code = %s, model = %s, name = %s, image_path = %s, shelf_date = %s, supplier = %s, cost_price = %s, remark = %s WHERE id = %s """ cursor.execute(sql, (code, model, name, image_path, shelf_date, supplier, cost_price, remark, product_id)) else: # 情况2:不更新图片路径(保留原图),但更新备注 sql = """ UPDATE products SET code = %s, model = %s, name = %s, shelf_date = %s, supplier = %s, cost_price = %s, remark = %s WHERE id = %s """ cursor.execute(sql, (code, model, name, shelf_date, supplier, cost_price, remark, product_id)) conn.commit() return True, None except Exception as e: conn.rollback() return False, str(e) finally: if 'cursor' in locals(): cursor.close() if conn: conn.close()
3. 修改主程序(app.py)
修改add_product路由:
python
@app.route('/add', methods=['GET', 'POST']) def add_product(): # ... GET请求处理不变 ... if request.method == 'POST': # 1. 获取表单数据 code = request.form.get('code', '').strip() model = request.form.get('model', '').strip() name = request.form.get('name', '').strip() shelf_date = request.form.get('shelf_date', '') supplier = request.form.get('supplier', '').strip() cost_price = request.form.get('cost_price', '') remark = request.form.get('remark', '').strip() # 获取备注 # ... 校验逻辑 ... # 调用数据访问层 success, result = dao.create_product( code=code, model=model, name=name, image_path=image_path, shelf_date=shelf_date, supplier=supplier, cost_price=cost_price, remark=remark if remark else None # 添加备注参数 ) # ... 后续处理 ...修改edit_product路由:
python
@app.route('/edit/<int:product_id>', methods=['GET', 'POST']) def edit_product(product_id): # ... 获取商品信息 ... if request.method == 'POST': # 获取表单数据 code = request.form.get('code', '').strip() model = request.form.get('model', '').strip() name = request.form.get('name', '').strip() shelf_date = request.form.get('shelf_date', '') supplier = request.form.get('supplier', '').strip() cost_price = request.form.get('cost_price', '') remark = request.form.get('remark', '').strip() # 获取备注 # ... 校验逻辑 ... # 调用数据访问层 success, error_msg = dao.update_product( product_id=product_id, code=code, model=model, name=name, image_path=new_image_path, shelf_date=shelf_date, supplier=supplier, cost_price=cost_price, remark=remark if remark else None # 添加备注参数 ) # ... 后续处理 ...4. 修改模板文件(add_product.html)
在成本价字段后面添加备注字段:
html
<div class="mb-3"> <label class="form-label">成本价 *</label> <input type="number" step="0.01" name="cost_price" class="form-control" value="{{ product.cost_price if product else '' }}" required> </div> <!-- 添加备注字段 --> <div class="mb-3"> <label class="form-label">备注</label> <textarea name="remark" class="form-control" rows="4" placeholder="请输入商品备注信息...">{{ product.remark if product else '' }}</textarea> <small class="text-muted">可输入商品详细说明、注意事项等(最多65535字符)</small> </div> <button type="submit" class="btn btn-primary">{{ "更新" if is_edit else "添加" }}</button>5. 修改列表页面(index.html)
在表格中添加备注列(可选):
html
<thead> <tr> <th>ID</th> <th>图片</th> <th>商品码</th> <th>型号</th> <th>名称</th> <th>上架日期</th> <th>进货商</th> <th>成本价</th> <th>备注</th> <!-- 新增备注列 --> <th>操作</th> </tr> </thead> <tbody> {% for product in products %} <tr> <!-- ... 其他字段 ... --> <td>{{ product.remark|truncate(50) if product.remark else '-' }}</td> <!-- 备注列,限制显示50字符 --> <!-- ... 操作按钮 ... --> </tr> {% endfor %} </tbody>数据类型选择建议:
TEXT: 最多 65,535 字符(约64KB)- 适合一般备注
MEDIUMTEXT: 最多 16,777,215 字符(约16MB)- 适合较长内容
LONGTEXT: 最多 4,294,967,295 字符(约4GB)- 适合超长内容
如果备注可能很长,建议使用MEDIUMTEXT:
sql
remark MEDIUMTEXT COMMENT '商品备注信息,支持长文本'
总结如下:
添加备注字段需要修改的文件清单
📋 需要修改的文件(共5个)
1.products.sql- 数据库表结构
位置:
/home/x79/pycode/projectShangpin3/products.sql修改内容:
在
CREATE TABLE语句中添加remark TEXT字段或者添加
ALTER TABLE语句(如果表已存在)
2.product_dao.py- 数据访问层
位置:
/home/x79/pycode/projectShangpin3/product_dao.py需要修改的函数(共4个):
get_all_products()- SELECT 语句添加 remark 字段get_product_by_id()- SELECT 语句添加 remark 字段create_product()- INSERT 语句添加 remark 参数update_product()- UPDATE 语句添加 remark 参数
3.app.py- 主应用程序
位置:
/home/x79/pycode/projectShangpin3/app.py需要修改的路由(共2个):
add_product()- POST请求中获取 remark 表单数据并传递edit_product()- POST请求中获取 remark 表单数据并传递
4.add_product.html- 添加/编辑表单模板
位置:
/home/x79/pycode/projectShangpin3/templates/add_product.html修改内容:
在成本价字段后面添加备注文本框(textarea)
5.index.html- 商品列表模板
位置:
/home/x79/pycode/projectShangpin3/templates/index.html修改内容(可选):
表格头部添加"备注"列
表格行中添加备注数据显示
📊 修改优先级
| 优先级 | 文件 | 是否必须 | 说明 |
|---|---|---|---|
| 🔴 必须 | products.sql | ✅ 是 | 数据库需要添加字段 |
| 🔴 必须 | product_dao.py | ✅ 是 | 程序需要读写备注字段 |
| 🔴 必须 | app.py | ✅ 是 | 需要获取和传递备注数据 |
| 🔴 必须 | add_product.html | ✅ 是 | 用户需要输入备注 |
| 🟡 可选 | index.html | ⚠️ 建议 | 列表页显示备注信息 |