检测是否从 psycopg2 游标中获取?

编程入门 行业动态 更新时间:2024-10-24 18:23:08
本文介绍了检测是否从 psycopg2 游标中获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我执行以下命令.

insert into hello (username) values ('me')

我就这样跑

cursor.fetchall()

我收到以下错误

psycopg2.ProgrammingError: no results to fetch

如何在不检查查询是插入"还是选择"的情况下检测是否调用 fetchall()?

How can I detect whether to call fetchall() or not without checking the query is "insert" or "select"?

谢谢.

推荐答案

看这个属性:

cur.description

执行查询后,如果没有返回行,它将被设置为 None,否则将包含数据 - 例如:

After you have executed your query, it will be set to None if no rows were returned, or will contain data otherwise - for example:

(Column(name='id', type_code=20, display_size=None, internal_size=8, precision=None, scale=None, null_ok=None),)

捕获异常并不理想,因为在某些情况下您可能会覆盖真正的异常.

Catching exceptions is not ideal because there may be a case where you're overriding a genuine exception.

更多推荐

检测是否从 psycopg2 游标中获取?

本文发布于:2023-10-16 12:15:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游标

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!