python - 通过 psycopg2 确定异常类型
问题描述
我将 python 与 psycopg2 一起使用。以下代码段会产生某种错误,但我无法获得任何输出,因此我无法处理该错误
cur = conn.cursor()
try:
cur.execute("INSERT INTO mqtt.records(client, id_type, value) VALUES (%(str)s, %(int)s, %(float)s)", (topic[1], switchCase(topic[-1]), msg.payload)
except psycopg2.Error as e:
print(e)
conn.commit()
cur.close()
我很确定这是某种类型转换错误,但它没有被except psycopg2.Error as e:
. 如果我使用一般except:
来捕获任何异常,它会捕获。但后来我不知道如何获取错误消息
解决方案
使用except Exception as e:
我收到消息“参数不能混合”,这导致我认为这... VALUES (%(str)s, %(int)s, %(float)s)
不起作用。
相反,我不得不对(str(topic[1]), int(switchCase(topic[-1])), float(msg.payload))
部分进行类型转换。
所以我基本上误解了文档。我真丢脸。
推荐阅读
- wordpress - 只显示给定单词的简码一次
- php - 在中间件中获取 laravel 路由 {slug}
- python - 在有向无环图 Python 中面临句法问题
- sql - 在 Oracle 中依靠 UNION
- html - 使标签的整个宽度可点击和响应
- performance - 如何通过 jenkins - jmeter webdriver 脚本管理 Web 驱动程序?
- python - 根据 SPARQL 查询和 Pagerank 值突出显示 draw_networkx 可视化中的节点
- anylogic - 是否有任何内置函数可以在anylogic中获取某个直径范围内的节点(点节点)列表?
- javascript - HTML5 Canvas 笔画文本重叠加入阿拉伯语、印地语、孟加拉语等语言的部分
- reactjs - 如何在 React 中处理表格中的滚动条事件