python - Python 与 Oracle 数据库
问题描述
我在使用 Python-3 和 cx-oracle 的 Oracle-12c 上。
如何处理以下 SQL 的“No rows returned”查询?如果没有返回行,我希望将字符串“Inactive”分配给结果。
cursor.execute("select STATUS from DOMAINTABLE")
for result in cursor:
print(result[0])
row = result.fetchone()
if row == None:
break
print ("Inactive")
解决方案
您应该遍历光标或调用该fetchone
方法,但不能同时执行以下操作:
cursor.execute("select STATUS from DOMAINTABLE")
for status, in cursor:
print(status)
break
else:
print('Inactive')
或者:
cursor.execute("select STATUS from DOMAINTABLE")
row = result.fetchone()
if row is None:
print('Inactive')
else:
print(row[0])
推荐阅读
- react-native - 如何在本机反应中同时水平和垂直两个方向滚动?
- entity-framework-core - 实体框架核心 IEntityTypeConfiguration 类是否具有 HasPrecision() 功能?
- bash - 如何在bash中的字符串后面回显一个变量?
- python - 为什么 Abaqus python 脚本没有从元素集中打印正确的最大应力?
- excel - 使用 PowerShell 修剪 Excel 单元格
- java - Java 字符串转日期格式
- ios - 如何在 Swift 中不断更新 ULabel 的文本
- c# - 为自动完成控件绑定继承列表
- javascript - 在第 8 面墙 aframe-js 中播放动画时,动画播放有延迟
- python - 为什么使用全局变量给出未定义?