通过cx_Oracle从用户定义的类型中提取数据

时间:2018-03-04 09:47:03

标签: python oracle cx-oracle user-defined-types

我试图通过cx_Oracle从用户定义类型(UDT)中提取数据。这是UDT结构:

CREATE OR REPLACE TYPE graphic_component AS OBJECT (
   type             NUMBER(6),
   source_type      NUMBER(4),
   meta_type_id     VARCHAR2(50 CHAR),
   name             VARCHAR2(100 CHAR),
   extension_info   VARCHAR2(500 CHAR),
   symbology_tokens VARCHAR2(2000 CHAR)
);

CREATE OR REPLACE TYPE graphic_component_array AS
      VARRAY (10000) OF graphic_component;

以下是使用Python的示例:

>>>insert = cursor.execute("SELECT COMPLEX FROM GRAPHIC WHERE ID=48662511087446403855368")
>>>complex = insert.fetchall()
[(<cx_Oracle.Object SCHEMA.GRAPHIC_COMPONENT_ARRAY at 0x33d71d0>,)]
>>>dir(complex)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'aslist', 'copy', 'delete', 'exists', 'extend', 'first', 'getelement', 'last', 'next', 'prev', 'setelement', 'size', 'trim', 'type']
>>>complex.first()
0

您可以看到用户定义的类型很好但我无法从此UDT或任何其他UDT中提取数据。我试图检查对象的所有属性,我发现方法first(),getelement()和last()。但是这些函数只返回0。

有没有办法如何通过cx_Oracle从UDT中提取数据,或者还有其他选项如何获取数据。

更新

<cx_Oracle.Object GRAPHIC_COMPONENT_ARRAY at 0x26c9f10>
Traceback (most recent call last):
  File "C:/Users/petr.silhak/PycharmProjects/migration-to-postgresql/test_parsing.py", line 34, in <module>
    print(ObjectRepr(complex[0][0]))
  File "C:/Users/petr.silhak/PycharmProjects/migration-to-postgresql/test_parsing.py", line 9, in ObjectRepr
    value = ObjectRepr(value)
  File "C:/Users/petr.silhak/PycharmProjects/migration-to-postgresql/test_parsing.py", line 14, in ObjectRepr
    value = getattr(obj, attr.name)
cx_Oracle.DatabaseError: DPI-1014: conversion between Oracle type 2010 and native type 3000 is not implemented

1 个答案:

答案 0 :(得分:0)

Here是处理cx_Oracle中复杂类型的代码段 摘录:

def ObjectRepr(obj):
    if obj.type.iscollection:
        returnValue = []
        for value in obj.aslist():
            if isinstance(value, cx_Oracle.Object):
                value = ObjectRepr(value)
            returnValue.append(value)
    else:
        returnValue = {}
        for attr in obj.type.attributes:
            value = getattr(obj, attr.name)
            if value is None:
                continue
            elif isinstance(value, cx_Oracle.Object):
                value = ObjectRepr(value)
            returnValue[attr.name] = value
    return returnValue  

您使用ObjectRepr(complex[0][0]),如果len(complex)>0,当然