python - AttributeError: 'pyodbc.Row' object has no attribute 'JobCount'

python - AttributeError: 'pyodbc.Row' object has no attribute 'JobCount'

我有一个查询从数据库中获取数据并返回值以便我可以解析。

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'

如果要检查 'JobCount' 是否是 query_data 的属性,请使用 hasattr()

if hasattr(query_data, 'JobCount'):
    print query_data.JobCount
else:
    send_status = send_data(job_data)
    print send_status