简体   繁体   English

Python 数据库连接:值错误:<cx_Oracle.LOB object at 0x000001CB4819E5A0> 不是 unicode 或序列

[英]Python DB Connection: ValueError: <cx_Oracle.LOB object at 0x000001CB4819E5A0> is not unicode or sequence

Hey I am pulling a date field from Oracle DB using the cx_Oracle module.嘿,我正在使用 cx_Oracle 模块从 Oracle DB 中提取日期字段。 The redacted query and connection module are:编辑的查询和连接模块是:

def getInitialData():   
    print("Gathering... ")     
    dsn_tns = cx_Oracle.makedsn('xyz.com', '1234', service_name='DB')
    conn = cx_Oracle.connect(user=r'me', password='password', dsn=dsn_tns) 
    SQLquery = ("""
SELECT REPORTDATE, 

FROM LONGDESCRIPTION 
WHERE 
       REPORTDATE > TO_DATE('01/01/2015 0:00:00', 'MM/DD/YYYY HH24:MI:SS'))""") 
    datai = pd.read_sql(SQLquery, conn)
    datai['REPORTDATE'] = pd.to_datetime(datai['REPORTDATE'], format='%m-%d-%Y')
    print("Data Retrieved")
    return datai

However, when I try to manipulate this later via:但是,当我稍后尝试通过以下方式操作时:

writer = index.writer()
print("Adding Data, this may take a moment... ")
for i in range(len(initialData)):      
    writer.add_document(docId=initialData.iloc[i]['CONTENTUID'], \
                        content=initialData.iloc[i]['LOWER(LDTEXT)'], \
                        date=initialData.iloc[i]['REPORTDATE'])
writer.commit()

I get:我得到:

ValueError: <cx_Oracle.LOB object at 0x000001CB4819E5A0> is not unicode or sequence

Has anyone seen this error?有没有人看到这个错误? Nothing in documentation/Google about it.文档/谷歌中没有关于它的任何内容。 How does it happen?它是如何发生的? It is weird to me because I am able to get this to work using a different datefield.这对我来说很奇怪,因为我能够使用不同的日期字段来让它工作。 Both show dtype of datetime64[ns]两者都显示datetime64[ns]

Since this is a data conversion issue, knowing the character sets in use would have been useful info.由于这是一个数据转换问题,了解正在使用的字符集将是有用的信息。

Some thoughts:一些想法:

  1. Set the character set when you connect.连接时设置字符集 Use the appropriate character set for your data.:为您的数据使用适当的字符集。:

     connection = cx_Oracle.connect(connectString, encoding="UTF-8", nencoding="UTF-8")

    You only need to use nencoding if you have NCHAR / NVARCHAR / NCLOB columns.如果您有 NCHAR / NVARCHAR / NCLOB 列,则只需要使用nencoding

  2. For 'small' LOBs (that are < 1GB and fit in cx_Oracle memory), you probably want to fetch them directly as strings , since this is faster.对于“小”LOB(小于 1GB 且适合 cx_Oracle 内存),您可能希望将它们直接作为字符串获取,因为这样速度更快。 Add a type handler:添加类型处理程序:

     def OutputTypeHandler(cursor, name, defaultType, size, precision, scale): if defaultType == cx_Oracle.CLOB: return cursor.var(cx_Oracle.LONG_STRING, arraysize=cursor.arraysize) if defaultType == cx_Oracle.BLOB: return cursor.var(cx_Oracle.LONG_BINARY, arraysize=cursor.arraysize)
  3. Check if you have corrupted data that can't be handled in the character sets.检查您是否有无法在字符集中处理的损坏数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM