简体   繁体   中英

Incorporate try except block with python code

I'm populating a spreadsheet with multiple queries that return a key and a corresponding value. Two database connections are being used in the script to return data.

The following code works perfectly if there is not an issue with the SQL statement being executed. If there is a problem with the SQL the programs exit with errors and my spreadsheet isn't populated.

If there is an error, I would like to still populate the key in the first column and have the value return '0'

Tricky part is since there are two server connections, one server might handle the SQL perfectly fine and the other might throw an exception.

Is there a way this code block can be converted to handle that?

I'm a bit of a Python newb, so delving into error handling is a bit daunting

# Populate the spreadsheet with data from the first set of date ranges.
row = 1
col = 0

for key, value in Queries.query_dic.iteritems():
    cur.execute(value.format(from_dateA,to_dateA))
    cur2.execute(value.format(from_dateA,to_dateA))
    rows = cur.fetchall()
    rows2 = cur2.fetchall()

    # Populate metric being queried in our horizontal headers
    worksheet[index].write(row, col, key, format)
    worksheet[index + 1].write(row, col, key, format)

    # Iterate over the data and write it out row by row.
    for return_count in rows:
        worksheet[index].write(row, col + 1, return_count[0], format2)

    for return_count in rows2:
        worksheet[index + 1].write(row, col + 1, return_count[0], format2)

    row += 1

To use a try-except you can do this

try:
    for key, value in Queries.query_dic.iteritems():
        cur.execute(value.format(from_dateA,to_dateA))
        cur2.execute(value.format(from_dateA,to_dateA))
        rows = cur.fetchall()
        rows2 = cur2.fetchall()

        # Populate metric being queried in our horizontal headers
        worksheet[index].write(row, col, key, format)
        worksheet[index + 1].write(row, col, key, format)

        # Iterate over the data and write it out row by row.
        for return_count in rows:
            worksheet[index].write(row, col + 1, return_count[0], format2)

        for return_count in rows2:
            worksheet[index + 1].write(row, col + 1, return_count[0], format2)

        row += 1
except:
    # Error Handling

If you want to have error-specific handling, you can simply put the error after the except

except KeyboardInterrupt:
    # Error Handling

After some trial and error, I found a solution that works.

# Print metric headers and counts for first date range.
row = 1
col = 0

for key, value in Queries.query_dic.iteritems():

    # Write the metrics headers
    worksheet[index].write(row, col, key, format)
    worksheet[index + 1].write(row, col, key, format)

    # Populate spreadsheet with count returned from query for Environment 1. 
    try:
        cur.execute(value.format(from_dateA,to_dateA))
        rows = cur.fetchall()
        for return_count in rows:
            worksheet[index].write(row, col + 1, return_count[0], format2)
    # Query will fail if module doesn't exist; in this case print '0'.
    except:
        worksheet[index].write(row, col + 1, '0', format2)

    # Populate spreadsheet with count returned from query for Environment 2. 
    try:
        cur2.execute(value.format(from_dateA,to_dateA))
        rows = cur2.fetchall()
        for return_count in rows:
            worksheet[index + 1].write(row, col + 1, return_count[0], format2)
    # Query will fail if module doesn't exist; in this case print '0'.
    except:
        worksheet[index + 1].write(row, col + 1, '0', format2)

    row += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM