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

使用cx_Oracle插入BLOB时遇WindowsPath不支持错误求助

解决 cx_Oracle.NotSupportedError: Python value of type WindowsPath not supported 问题

这个错误的原因很明确:cx_Oracle 并不直接支持 pathlib.WindowsPath 类型作为绑定参数插入BLOB列。你需要把路径对象转换成 cx_Oracle 能够识别的格式,下面提供两种可行的解决方案:

方案1:使用文件对象插入(推荐大文件)

这种方式不需要把整个文件加载到内存,适合处理较大的二进制文件,cx_Oracle会自动处理文件内容的写入:

import sys
import cx_Oracle
from pathlib import Path

username = 'EWORDPR'
password = 'EWORDPR'
databaseName = "10.11.201.161:1521/ORCL"

connection = cx_Oracle.connect(username, password, databaseName)
cursor = connection.cursor()

# 假设你的文件路径是这样获取的
my_file = Path("path/to/your/binary/file")

# 打开文件(注意用二进制模式)
with my_file.open("rb") as f:
    # 执行插入,直接绑定文件对象到BLOB列
    cursor.execute("INSERT INTO your_table (blob_column) VALUES (:1)", [f])
    connection.commit()

cursor.close()
connection.close()

方案2:读取文件二进制内容插入(适合小文件)

如果文件较小,可以直接读取文件的二进制字节数据,然后绑定到参数:

import sys
import cx_Oracle
from pathlib import Path

username = 'EWORDPR'
password = 'EWORDPR'
databaseName = "10.11.201.161:1521/ORCL"

connection = cx_Oracle.connect(username, password, databaseName)
cursor = connection.cursor()

my_file = Path("path/to/your/binary/file")

# 读取文件的二进制内容
file_content = my_file.read_bytes()

# 执行插入,绑定bytes类型数据
cursor.execute("INSERT INTO your_table (blob_column) VALUES (:1)", [file_content])
connection.commit()

cursor.close()
connection.close()

注意事项

  • 替换代码中的 your_table 和 blob_column 为你实际的表名和BLOB列名
  • 操作完成后记得提交事务(connection.commit()),否则数据不会写入数据库
  • 大文件优先选择方案1,避免内存占用过高

内容的提问来源于stack exchange,提问作者S.Rakin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:47:47