【发布时间】: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