python-AttributeError:';pyodbc.Row';对象没有属性';工作计数';
我有一个从数据库中获取数据并返回值的查询,这样我就可以进行分析了python-AttributeError:';pyodbc.Row';对象没有属性';工作计数';,python,pyodbc,Python,Pyodbc,我有一个从数据库中获取数据并返回值的查询,这样我就可以进行分析了 def executeScriptsFromFile(monitor): # Open and read the file as a single buffer fd = open(os.path.join(BASE_DIR, 'sql/{0}.sql'.format(monitor)), 'r') if args.alias: sql_query = fd.read().format("'
def executeScriptsFromFile(monitor):
# Open and read the file as a single buffer
fd = open(os.path.join(BASE_DIR, 'sql/{0}.sql'.format(monitor)), 'r')
if args.alias:
sql_query = fd.read().format("'" + args.alias + "'")
else:
sql_query = fd.read()
fd.close()
# Execute SQL query from the input file
cursor.execute(sql_query)
result = cursor.fetchone()
return result
查询可能会有所不同,因此我尝试构建逻辑,以便在JobCount
不是其中一个值时跳过该部分
query_data = executeScriptsFromFile(args.monitor)
print query_data
if query_data.JobCount:
print query_data.JobCount
else:
send_status = send_data(job_data)
print send_status
不幸的是,我得到了以下回溯。如果该值不存在,如何忽略该值
Traceback (most recent call last):
File "tidal-zabbix.py", line 92, in <module>
if query_data.JobCount:
AttributeError: 'pyodbc.Row' object has no attribute 'JobCount'
回溯(最近一次呼叫最后一次):
文件“tidal zabbix.py”,第92行,在
如果查询\u data.JobCount:
AttributeError:“pyodbc.Row”对象没有属性“JobCount”
如果要检查'JobCount'
是否是查询数据的属性
请使用hasattr()
如果要检查
'JobCount'
是否是query\u data
的属性,请使用hasattr()
您确定结果具有该属性吗?由于是DB查询,我假设您需要执行查询数据[0]、查询数据[1]等操作。您确定结果具有该属性吗?我假设由于是DB查询,您需要执行查询数据[0]、查询数据[1]等操作。或者,使用例如
getattr(查询数据'JobCount',False)
如果属性不存在,则获取默认值。@Dolda2000这样,您就无法区分appart属性不存在的情况和属性存在且具有默认值的情况。除非我们知道.JobCount
永远不会是False
,否则这很可能是真的,但我们不能真正确定。如果无论属性是否存在都要获取值,请使用getattr()
,如果要判断属性是否存在,请使用hasattr()
。或者,使用例如getattr(查询数据,'JobCount',False)
如果属性不存在,则获取默认值。@Dolda2000这样,您就无法区分appart属性不存在的情况和属性存在且具有默认值的情况。除非我们知道.JobCount
永远不会是False
,否则这很可能是真的,但我们不能真正确定。如果要获取值而不管属性是否存在,请使用getattr()
,如果要判断属性是否存在,请使用hasattr()
。
if hasattr(query_data, 'JobCount'):
print query_data.JobCount
else:
send_status = send_data(job_data)
print send_status