Skip to main content
 首页 » 编程设计

Python,Oracle DB,一列中的XML数据,获取cx_Oracle.Object

2024年10月17日8dudu

我正在使用 Python 从 Oracle 数据库中获取数据。所有行都有一个包含 XML 数据的列。当我使用 python 打印从 Oracle DB 获取的数据时,带有 XML 数据的列被打印为 - cx_Oracle.OBJECT 对象位于 0x7fffe373b960 等。我什至将数据转换为 pandas 数据框,但此列的数据仍被打印为 cx_Oracle。 0x7fffe373b960 处的 OBJECT 对象。我想访问存储在此列(XML 文件)中的键值数据。

请您参考如下方法:

请阅读内联评论。

cursor = connection.cursor() # you know what it is for 
 
# here getClobVal() returns whole xml. It won't work without alias I don't know why. 
query = """select a.columnName.getClobVal() from tablename a"""  
 
cursor.execute(query) #you know what it is for 
 
result = cursor.fetchone()[0].read() # for single record 
 
result = cursor.fetchall() # for all records 
for res in result:   
    print res[0].read()