【问题标题】:Handling exception when there are multiple sql queries to be fired触发多个 sql 查询时的异常处理
【发布时间】:2019-03-26 07:52:02
【问题描述】:

我正在使用四个选择查询从数据库中获取数据。数据的方式是在某些情况下选择查询的输入可能为空。在这种情况下,该特定选择语句不起作用是可以的。简而言之,我想要的是四个 select 语句应该触发,并且无论其他查询是否失败,任何语句都应该工作。

try:
    cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip_i)

except Exception as e:
    print("error while fetching details " + str(e))

result_i = cur.fetchall()

try:
    cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip_n)

except Exception as e:
    print("error while fetching details " + str(e))

result_n = cur.fetchall()

try:
    cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip_c)

except Exception as e:
    print("error while fetching details " + str(e))

result_c = cur.fetchall()

try:
    cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip_b)

except Exception as e:
    print("error while fetching details " + str(e))

result_b = cur.fetchall()

【问题讨论】:

  • 但是如果发生异常,你还是会执行fetchall ?
  • 使用for 循环!
  • ip_x 值放在一个列表中并遍历它。旁注:不要使用字符串格式来构建查询。
  • 创建一个循环并将 fetchall 结果存储在 list 中。
  • @mash 如果有人的回答对您有帮助,请点赞/接受答案。

标签: python mariadb pymysql try-except dbconnection


【解决方案1】:

是的,只要把它放在一个 for 循环中。

ip_list = [ip_i, ip_n, ip_c, ip_b]
result_list = []

for ip in ip_list:
    try:
        cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip)

    except Exception as e:
        print("error while fetching details " + str(e))        

    result_list.append(cur.fetchall())

我猜 cur.fetchall() 不会产生错误,如果它产生或者你不希望它运行,那么你可以把它放在 try 中。

所以我会把它改成这个来跟踪生成的错误;

ip_list = [ip_i, ip_n, ip_c, ip_b]
result_list = []

for ip in ip_list:
    try:
        cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip)
        result_list.append(cur.fetchall())

    except Exception as e:
        print("error while fetching details " + str(e))  
        result_list.append("ERROR")      

【讨论】:

  • result_list.append(cur.fetchall()) 将因异常而失败,或者可能给出重复值
  • 啊,我以为没有,因为他在原始代码中就是这样做的
  • 嗯,我想我们可以假设光标是全局的。我仍然认为append 应该在try 内完成,这样它们就会一起失败,而不是在查询失败时尝试调用fetchall()
  • 我的意思是这可以很容易地通过将附加移动到 try 内的 cur.execute 之后来解决
  • 是的,这就是我建议您在答案中编辑的内容:)
【解决方案2】:

将您的 IP 地址变量添加到列表中,然后遍历该列表。在这里,我使用traceback 模块打印整个堆栈跟踪(不仅仅是异常),并且我还在try 块内执行fetchall,否则,如果在@987654324 期间确实发生异常@你会试图获取任何东西。

import traceback

ip_list = [ ip_i, ip_n, ip_c, ip_b ]

for ip in ip_list:
    try:
        cur.execute("select IP_ADD,VENDOR,DVC_ROLE,CIRCLE,SSA,REGION from DVC_SUMMARY_DATA where IP_ADD in (%s);" % ip)

        result = cur.fetchall()

    except Exception as e:
        print(traceback.format_exc())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多