首页 > 解决方案 > 从 oracle 数据库中打印 Python 中的值

问题描述

在将从 oracle 表中检索到的 python 中的值显示到 CLOB 字段中时出现问题:

甲骨文查询:

SELECT EXTRACTVALUE(xmltype(t.xml), '/DCResponse/ResponseInfo/ApplicationId') 
  FROM table t 
 WHERE id = 2

Oracle 客户端中显示的值

5701200

Python代码

import cx_Oracle 
conn = cx_Oracle.Connection("user/pwd@localhost:1521/orcl")
cursor = conn.cursor()
cursor.execute("""SELECT EXTRACTVALUE(xmltype(t.xml),'/DCResponse/ResponseInfo/ApplicationId') FROM table t where id = 2""")
for row in cursor:
print(row)

Python 控制台:什么都不显示!!!我要显示:5701200

请帮忙。最好的问候吉安卡罗

标签: pythonoracle

解决方案


您的代码只有几个问题:

  • 替换cx_Oracle.Connectioncx_Oracle.connect
  • 注意与相关的缩进print(row)
  • SELECT 语句中的三重双引号是多余的,将它们替换为单双引号
  • 首选使用print(row[0])以返回所需的数字而不是打印的元组。

    import cx_Oracle 
    conn = cx_Oracle.connect('user/pwd@localhost:1521/orcl')
    cursor = conn.cursor()
    
    query  = "SELECT EXTRACTVALUE(xmltype(t.xml),'/DCResponse/ResponseInfo/ApplicationId')"
    query += "  FROM tab t "
    query += " WHERE t.ID = 2 "
    
    cursor.execute( query )
    for row in cursor:
        print(row[0])
    

如我的案例所述,将查询分配给变量不是必需的,但最好使用它来体面地显示长 SELECT 语句。


推荐阅读