处理错误消息psycopg2.ProgrammingError

问题描述 投票:0回答:1

如何处理类似psycopg2.ProgrammingError的错误:

   try
      cursor.execute("CREATE TEMP TABLE temp_table1 AS SELECT 11 as a, 22 as b, 'Count' as unit; DROP TABLE temp_table1;")
      data = cursor.fetchall()
   except psycopg2.ProgrammingError as e:
        if print(e) == "no result to fetch":
            print("Skip Error - {}".format(e)
        else:
            raise(e)

cursor之前已创建。

e.pgerror为None。

if print(e) == "no result to fetch"无效

我将运行带有返回值的不同脚本,而没有返回值。脚本什么也不返回时如何处理?

UPDATE:cursor.description工作方式

   try
      cursor.execute("CREATE TEMP TABLE temp_table1 AS SELECT 11 as a, 22 as b, 'Count' as unit; DROP TABLE temp_table1;")
      if cursor.description:
            data = cursor.fetchall()
   except psycopg2.ProgrammingError as e:
        if print(e) == "no result to fetch":
            print("Skip Error - {}".format(e)
        else:
            raise(e)
python-3.x psycopg2
1个回答
0
投票
cursor.description正在工作
© www.soinside.com 2019 - 2024. All rights reserved.