You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求实现:通过指定ID自动从关联MySQL表下载文件

自动化从MySQL关联表下载文件的实现方案

需求概述

指定一个id值,通过以下三步自动完成文件下载:

  • 从wims_detections表中,根据输入的id获取对应的detection_id
  • 利用得到的detection_id,在sensors_detections_data表中查询对应的data_id
  • 通过data_id在sensors_data表中取出data字段存储的文件内容,保存为本地文件

手动执行SQL示例

-- 第一步:获取detection_id
SELECT detection_id FROM wims_detections WHERE id = '715696';

-- 第二步:获取data_id
SELECT data_id FROM sensors_detections_data WHERE detection_id = 'TY-LU-W1_20231221T155059823+0300';

-- 第三步:获取文件数据
SELECT data FROM sensors_data WHERE data_id = 'TY-TY-SL1_20231221T155059878+0300';

关联表结构示意图

关联表结构示意图

自动化实现脚本(Python)

以下脚本只需修改数据库连接参数和输入目标id,即可自动完成查询与文件下载:

import mysql.connector
from mysql.connector import Error

def download_file_by_id(target_id, save_path="downloaded_file.bin"):
    # 数据库连接参数,根据实际情况修改
    db_config = {
        'host': 'your_host',
        'database': 'your_database',
        'user': 'your_username',
        'password': 'your_password'
    }
    connection = None
    try:
        # 连接数据库
        connection = mysql.connector.connect(**db_config)
        if connection.is_connected():
            cursor = connection.cursor()

            # 查询detection_id
            cursor.execute("SELECT detection_id FROM wims_detections WHERE id = %s", (target_id,))
            result = cursor.fetchone()
            if not result:
                print(f"未找到id为{target_id}的记录")
                return
            detection_id = result[0]
            print(f"获取到detection_id: {detection_id}")

            # 查询data_id
            cursor.execute("SELECT data_id FROM sensors_detections_data WHERE detection_id = %s", (detection_id,))
            result = cursor.fetchone()
            if not result:
                print(f"未找到detection_id为{detection_id}的data_id记录")
                return
            data_id = result[0]
            print(f"获取到data_id: {data_id}")

            # 查询文件数据并保存
            cursor.execute("SELECT data FROM sensors_data WHERE data_id = %s", (data_id,))
            result = cursor.fetchone()
            if not result:
                print(f"未找到data_id为{data_id}的文件数据")
                return
            file_data = result[0]

            # 保存文件到本地
            with open(save_path, 'wb') as f:
                f.write(file_data)
            print(f"文件已成功保存到: {save_path}")

    except Error as e:
        print(f"数据库操作错误: {e}")
    finally:
        # 关闭数据库连接
        if connection and connection.is_connected():
            cursor.close()
            connection.close()

# 调用示例:指定目标id和保存路径
download_file_by_id(target_id='715696', save_path='my_downloaded_file.bin')

使用说明

  1. 确保已安装mysql-connector-python:执行pip install mysql-connector-python
  2. 修改脚本中的数据库连接参数(host、database、user、password)
  3. 调用download_file_by_id函数时传入目标id和期望的文件保存路径

内容的提问来源于stack exchange,提问作者kyrs161

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:22:48