簡體   English   中英

將try塊與try代碼合並在一起

[英]Incorporate try except block with python code

我正在用多個返回鍵和相應值的查詢填充電子表格。 腳本中使用了兩個數據庫連接來返回數據。

如果執行的SQL語句沒有問題,則以下代碼可完美工作。 如果SQL存在問題,則程序會退出並顯示錯誤,並且不會填充我的電子表格。

如果有錯誤,我仍想在第一列中填充鍵,並使值返回“ 0”

棘手的是,因為有兩個服務器連接,所以一台服務器可以很好地處理SQL,而另一台則可以引發異常。

有沒有一種方法可以將該代碼塊轉換為處理該代碼塊?

我有點像Python newb,所以研究錯誤處理有點令人生畏

# 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

要使用try-except,可以執行此操作

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

如果要進行特定於錯誤的處理,只需將錯誤放在“ except”之后

except KeyboardInterrupt:
    # Error Handling

經過反復試驗,我找到了可行的解決方案。

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM