首先,我正在处理的SQL:
CREATE OR REPLACE TYPE address_t AS OBJECT
(
street VARCHAR2(80),
city VARCHAR2(80),
state_abbr CHAR(2),
zip_code DECIMAL(5)
)
/
CREATE TABLE branch
(
id DECIMAL(12) NOT NULL,
address address_t
PRIMARY KEY(id)
)
/
我已经验证数据库中的数据是正确的(在输入一些测试数据之后)。我现在尝试编写JSONEncoder
来解析Oracle address_t
类型作为可序列化的JSON类型。这是我一直在玩的功能:
class ObjectParser(json.JSONEncoder):
def default(self, o):
if str(type(o)) == "<type 'cx_Oracle.OBJECT'>":
print "Zipcode: %s" % o.ZIP_CODE
if str(o.type).find("ADDRESS_T") != -1:
return { "street" : o.STREET, "city": o.CITY,
"state_abbr": o.STATE_ABBR, "zip_code": o.ZIP_CODE }
elif str(o.type).find("PHONENUMBER_T") != -1:
return { "area_code" : o.AREA_CODE,
"phone_number": o.PHONE_NUMBER }
return json.JSONEncoder.default(self, o)
# ...
con = getdb()
cur = con.cursor()
cur.execute("SELECT * FROM BRANCH")
res = cursor.fetchall()
print json.dumps(res, cls=ObjectParser)
在它显示Zipcode:
的行上,我已经拥有了address_t
类型的所有其他属性,并且它们打印得很好。有什么想法吗?
这是实际错误:
File "query.py", line 14, in default
print "Zipcode: %s" % o.ZIP_CODE
cx_Oracle.NotSupportedError: ExternalObjectVar_GetAttributeValue():
unhandled data type 7