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)
dssdsasdsa
最新推荐文章于 2024-09-28 14:02:07 发布