python - 从 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
请帮忙。最好的问候吉安卡罗
解决方案
您的代码只有几个问题:
- 替换
cx_Oracle.Connection
为cx_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 语句。
推荐阅读
- javascript - 对象作为 React 子级无效(找到:[object Promise])。当我想将状态设置为 true 时显示错误
- amazon-web-services - 适用于 AWS Secrets Manager 的 VPC 终端节点
- android - Java 代码中的 Android 11 文件读/写权限问题
- php - 有没有办法来回更改输入文本和选择表单?
- string - 错误:对于 saleor-platform_db_1 需要一个类似字节的对象,而不是“str”
- php - Laravel 8:目标类 [ProductController] 不存在
- r - 强制固定效应的总和为零
- c++ - 根据输入参数在有限和无限循环之间切换
- cherrypy - cherrypy 未通过 cherrpy.system.exit 退出
- python - 如何将实时数据抓取到谷歌表格中