计时
评论
收藏

计时

经验分享
v17513244774
2025-10-31 08:59·浏览量:191
v17513244774
影刀专家
发布于 2025-10-30 20:32更新于 2025-10-31 08:59191浏览

# 使用提醒:

# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能

# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能

# 3. 当此模块作为流程独立运行时执行main函数

# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot

import pymysql

from datetime import datetime

from xbot import print, sleep

from .import package

from .package import variables as glv

def connect_to_mysql():

   """

   连接到MySQL数据库

   """

   try:

       # 数据库连接信息

       host = '123.456.123'

       port = 3306

       user = '账号'

       password = '密码'

       db = '表'

       

       # 建立数据库连接

       connection = pymysql.connect(

           host=host,

           port=port,

           user=user,

           password=password,

           db=db,

           charset='utf8mb4',

           cursorclass=pymysql.cursors.DictCursor

       )

       print("数据库连接成功!")

       return connection

   except Exception as e:

       print(f"数据库连接失败: {e}")

       return None

def insert_operation_report(connection, operator_name, start_time, end_time, execution_time, app_name='RPA系统', description='', **kwargs):

   """

   向rpa_operation_report表插入一行数据

   

   参数:

       connection: 数据库连接

       app_name: 应用名称

       operator_name: 操作者名称

       start_time: 开始时间

       end_time: 结束时间

       execution_time: 执行时间

       description: 描述(默认为空)

       **kwargs: 其他字段参数

   """

   if not connection:

       return False

   

   try:

       with connection.cursor() as cursor:

           # 首先查询表结构,了解有哪些字段

           cursor.execute("DESCRIBE rpa_operation_report")

           table_structure = cursor.fetchall()

           

           # 提取可用的字段名

           available_fields = [field['Field'] for field in table_structure]

           print(f"可用的字段: {available_fields}")

           

           # 根据实际表结构构建插入语句

           field_names = []

           placeholders = []

           values = []

           

           # 主要参数字段

           main_fields = {

               'app_name': app_name,

               'operator_name': operator_name,

               'start_time': start_time,

               'end_time': end_time,

               'execution_time': execution_time,

               'description': description,

               'create_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),

               'update_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S')

           }

           

           # 只使用表中实际存在的字段

           for field, value in main_fields.items():

               if field in available_fields:

                   field_names.append(field)

                   placeholders.append('%s')

                   values.append(value)

           

           # 处理其他自定义字段

           for field, value in kwargs.items():

               if field in available_fields:

                   field_names.append(field)

                   placeholders.append('%s')

                   values.append(value)

           

           if not field_names:

               print("错误:表中没有可用的字段")

               return False

           

           # 构建SQL语句

           sql = f"""

           INSERT INTO rpa_operation_report

           ({', '.join(field_names)})

           VALUES ({', '.join(placeholders)})

           """

           

           # 执行插入

           cursor.execute(sql, values)

           connection.commit()

           

           print(f"数据插入成功!插入ID: {cursor.lastrowid}")

           print(f"插入的字段: {field_names}")

           print(f"插入的值: {values}")

           return True

   except Exception as e:

       print(f"数据插入失败: {e}")

       connection.rollback()

       return False

def main(app_name, operator_name, start_time, end_time, execution_time, description=''):

   """

   主函数,插入数据到rpa_operation_report表

   

   参数:

       app_name: 应用名称

       operator_name: 操作者名称

       start_time: 开始时间

       end_time: 结束时间

       execution_time: 执行时间

       description: 描述(默认为空)

   """

   # 连接数据库

   connection = connect_to_mysql()

   if not connection:

       return False

   

   try:

       # 插入数据,传递所有必需参数

       result = insert_operation_report(

           connection,

           operator_name,

           start_time,

           end_time,

           execution_time,

           app_name,

           description

       )

       

       return result

   finally:

       # 关闭连接

       if connection:

           connection.close()

           print("\n数据库连接已关闭")


收藏1
全部评论1
最新
发布评论
评论