cx_Oracle.NotSupportedError: Python value of type WindowsPath not supported

cx_Oracle.NotSupportedError: Python value of type WindowsPath not supported

我正在使用 cx_Oracle 将目录中的二进制文件作为 BLOB 插入 table 的列中。

因此,我打算使用 python3 的 PATH 模块,然后通过使用 WindowsPath 获取文件来插入二进制文件。这是示例:-

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) # Connection Established

cursor=connection.cursor()

my_file = Path("22 Apr 2018.txt")
print("File exists:",my_file.is_file()) #File Exists: True

rows = [ ('01787440110', 'Rakin','22-04-2017', my_file )]
cursor.executemany('insert into CHAT_HISTORY(MOBILE_NUMBER,USER_NAME,CHAT_DATE,USER_TRANSCRIPTS) values (:1, :2, :3, :4)', rows)

connection.commit()

cursor.close()

connection.close()

然后,我收到以下错误:-

Traceback (most recent call last): File "F:/Python with ORACLE/orapy.py", line 53, in print(cursor.executemany('insert into CHAT_HISTORY(MOBILE_NUMBER,USER_NAME,CHAT_DATE,USER_TRANSCRIPTS) values (:1, :2, :3, :4)', rows)) cx_Oracle.NotSupportedError: Python value of type WindowsPath not supported.

有什么方法可以从目录导出二进制文件,然后使用 cx_Oracle 直接将文件作为 BLOB 插入?

由于文件可以包含任何类型的数据,因此您必须自己打开并阅读其中的内容。使用以下方法可以很容易地完成此操作:

rows = [ ('01787440110', 'Rakin','22-04-2017', my_file.open("rb").read() )]

这对于容易放入内存的文件非常有效,对于那些类型的文件,这是最有效的方法。对于非常大的文件,您将需要使用 LOB 定位器并改为流式传输数据。