psycopg2 : cursor already closed

前端 未结 2 945

I am using psycopg2 2.6.1. I have a bunch of queries that I need to execute in sequence.

conn = psycopg2.connect(database=redshift_database,
                


        
2条回答
  •  被撕碎了的回忆
    2021-02-07 23:10

    You should explicitely regenerate the cursor in the except bloc in case something went wrong at a lower level that the query:

    for query in queries:
        try:
            cursor.execute(query)
        except:
            print e.message
            try:
                cursor.close()
                cursor = conn.cursor()
            except:
                conn.close()
                conn = psycopg2.connect(...)
            cursor = conn.cursor()
    

提交回复
热议问题