dssdsasdsa

import subprocess
import chunkify

def insert_image_into_db(image_path, issue_id, data_category):
    # 构建SQL*Plus命令
    sqlplus_command = [
        "sqlplus",
        "-S",  # 静默模式
        "abnw/abnwtest@//10.51.235.98:1521/orcl",
        "BEGIN :blob := EMPTY_BLOB(); END;"  # 先声明BLOB变量
    ]
    # 读取图片并分割成小块
    with open(image_path, 'rb') as file:
        image_data = file.read()
        chunks = chunkify.chunkify(image_data, max_chunk_size=2400)  # 使用自定义的chunk方法分割图片数据,每个chunk大小约为2400字节
    # 构建SQL命令
    sql_commands = f"""
    DECLARE 
        v_offset NUMBER := 1; 
    BEGIN 
        FOR i IN 1..{len(chunks)} LOOP 
            DBMS_LOB.WRITE(:blob, LENGTH(chunks[i]), v_offset, UTL_I18N.STRING_TO_RAW(chunks[i], 'HEX')); 
            v_offset := v_offset + LENGTH(chunks[i]); 
            IF i = {len(chunks)} THEN 
                INSERT INTO issue_analysis_image_data (ISSUE_ID, DATA_CATEGORY, FILE_DATA) VALUES ('{issue_id}', '{data_category}', :blob); 
                COMMIT; 
            END IF; 
        END LOOP; 
    END; 
    /"""
    # 执行SQL*Plus命令
    process = subprocess.Popen(sqlplus_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    stdout, stderr = process.communicate(input=sql_commands)
    if process.returncode != 0:
        print(f"Error executing SQL*Plus command: {stderr}")
    else:
        print("Image inserted successfully.")
        print("SQL*Plus output:")
        print(stdout)
        print("SQL*Plus error output:")
        print(stderr)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值