#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 简单测试:直接测试保存和查询 file_path """ import pymysql import os from datetime import datetime def test_file_path(): """测试 file_path 的保存和查询""" print("="*60) print("测试 file_path 保存和查询") print("="*60) conn = None try: # 连接数据库 print("\n1. 连接数据库...") conn = pymysql.connect( host='haoslm2.xicp.net', port=10216, user='root', password='DsideaL147258369', database='yltcharge', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) print(" ✓ 连接成功") cursor = conn.cursor() # 2. 插入测试数据 print("\n2. 插入测试数据...") test_id = int(datetime.now().timestamp() * 1000) test_file_path = '/reports/test_file_path.xlsx' print(f" 准备插入:") print(f" - ID: {test_id}") print(f" - file_path: '{test_file_path}'") insert_sql = """ INSERT INTO t_daily_report_history (id, report_date, start_time, end_time, split_type, split_value, total_orders, total_amount, file_path, status, create_time) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW()) """ cursor.execute(insert_sql, ( test_id, '2025-01-01', '2025-01-01 08:00:00', '2025-01-02 08:00:00', 'test', 'test_value', 10, 100.50, test_file_path, # 这里传入 file_path 1 )) conn.commit() print(" ✓ 插入成功") # 3. 查询数据 print("\n3. 查询刚插入的数据...") cursor.execute("SELECT * FROM t_daily_report_history WHERE id = %s", (test_id,)) result = cursor.fetchone() if result: print(" ✓ 查询成功") print(f" - ID: {result.get('id')}") print(f" - file_path: '{result.get('file_path')}'") print(f" - status: {result.get('status')}") if result.get('file_path') == test_file_path: print("\n✓✓✓ file_path 保存和查询正常!") else: print(f"\n❌❌❌ file_path 不匹配!") print(f" 期望: '{test_file_path}'") print(f" 实际: '{result.get('file_path')}'") else: print(" ❌ 查询失败,没有找到数据") # 4. 清理测试数据 print("\n4. 清理测试数据...") cursor.execute("DELETE FROM t_daily_report_history WHERE id = %s", (test_id,)) conn.commit() print(" ✓ 已清理") print("\n" + "="*60) print("测试完成") print("="*60) except Exception as e: print(f"\n❌ 测试失败: {e}") import traceback traceback.print_exc() finally: if conn: conn.close() input("\n按回车键退出...") if __name__ == "__main__": test_file_path()