import pymysql import sys # 数据库连接配置 db_config = { 'host': '218.29.165.242', 'port': 3306, 'user': 'root', 'password': 'Monitor@20240101', 'database': 'ehs_qianqiu', 'charset': 'utf8mb4' } # 查询hdcheck_plan表结构 sql_structure = """ SHOW FULL COLUMNS FROM zzmj_ehs_hdcheck_plan; """ # 查询hdcheck_plan表详细数据(使用正确的字段名) sql_data = """ SELECT hdcheckplan_id, ctrl_id, company_id, hd_content, check_cycle, check_cycle_unit, work_start_time, work_end_time, risk_unit_id, risk_object_id, risk_id, work_day_type, work_type, risk_object_name, risk_unit_name FROM zzmj_ehs_hdcheck_plan WHERE upload_status = 'I' LIMIT 3; """ try: # 连接数据库 conn = pymysql.connect(**db_config) cursor = conn.cursor(pymysql.cursors.DictCursor) # 1. 查询表结构 print("=" * 100) print("zzmj_ehs_hdcheck_plan 表结构") print("=" * 100) cursor.execute(sql_structure) columns = cursor.fetchall() print(f"{'字段名':<25} | {'类型':<30}") print("-" * 60) for col in columns: print(f"{col['Field']:<25} | {col['Type']:<30}") # 2. 查询数据 print("\n" + "=" * 150) print("查询数据") print("=" * 150) cursor.execute(sql_data) results = cursor.fetchall() if results: for idx, row in enumerate(results, 1): print(f"\n【第 {idx} 条记录】") print(f"hdcheckplan_id: {row.get('hdcheckplan_id', '')}") print(f"company_id: {row.get('company_id', '')}") print(f"ctrl_id: {row.get('ctrl_id', '')}") print(f"hd_content: {row.get('hd_content', '')}") print(f"check_cycle: {row.get('check_cycle', '')}") print(f"check_cycle_unit: {row.get('check_cycle_unit', '')}") print("-" * 100) print(f"work_start_time: {row.get('work_start_time', 'NULL')}") print(f"work_end_time: {row.get('work_end_time', 'NULL')}") print(f"risk_object_name: {row.get('risk_object_name', 'NULL')}") print(f"risk_unit_name: {row.get('risk_unit_name', 'NULL')}") print("-" * 100) print(f"risk_unit_id: {row.get('risk_unit_id', 'NULL')}") print(f"risk_object_id: {row.get('risk_object_id', 'NULL')}") print(f"risk_id: {row.get('risk_id', 'NULL')}") print(f"work_day_type: {row.get('work_day_type', 'NULL')}") print(f"work_type: {row.get('work_type', 'NULL')}") print("=" * 150) else: print("查询成功,但没有找到符合条件的记录。") # 关闭连接 cursor.close() conn.close() except pymysql.Error as e: print(f"数据库错误: {e}") sys.exit(1) except Exception as e: print(f"错误: {e}") sys.exit(1)