Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python psycopg2-';非类型';对象没有属性';回滚';_Python_Psycopg2 - Fatal编程技术网

Python psycopg2-';非类型';对象没有属性';回滚';

Python psycopg2-';非类型';对象没有属性';回滚';,python,psycopg2,Python,Psycopg2,我正在尝试使用pyspark代码中的psycopg2从postgresql表中删除记录。但是我犯了一个错误。不知道怎么了。提前谢谢 def delete_records(table,city_list,key): connection = None try: connection = psycopg2.connect(host=host, database=db,

我正在尝试使用pyspark代码中的psycopg2从postgresql表中删除记录。但是我犯了一个错误。不知道怎么了。提前谢谢

def delete_records(table,city_list,key):
    connection = None
    try: 
        connection = psycopg2.connect(host=host,
                                             database=db,
                                             user=user,
                                             password=password)
        cursor = connection.cursor()
        delete_query = "Delete from " +table+ " where "+key+" in "+ str(tuple(city_list))
        cursor.execute(delete_query)
        connection.commit()
        logger.debug("Record deleted successfully")
    except (Exception, psycopg2.DatabaseError) as error :
        logger.error("%s transction error Reverting all other operations of a transction ", error)
        connection.rollback()
    finally:
        if connection is not None:
            cursor.close()
            connection.close()
            logger.debug("PostgreSQL connection is closed")

delete_records(table_name,city_list,"id")
错误


请帮忙。提前感谢

在您尝试的第一行中可能出现错误,因此当您到达except时,连接仍然没有

def delete_records(table,city_list,key):
    connection = None
    try: 
        connection = psycopg2.connect(host=host,
                                             database=db,
                                             user=user,
                                             password=password)
        cursor = connection.cursor()
        delete_query = "Delete from " +table+ " where "+key+" in "+ str(tuple(city_list))
        cursor.execute(delete_query)
        connection.commit()
        logger.debug("Record deleted successfully")
    except (Exception, psycopg2.DatabaseError) as error :
        logger.error("%s transction error Reverting all other operations of a transction ", error)
        connection.rollback()
    finally:
        if connection is not None:
            cursor.close()
            connection.close()
            logger.debug("PostgreSQL connection is closed")

delete_records(table_name,city_list,"id")
正如您在评论中提到的,将
如果连接不是None:
添加到except块听起来是个好主意

您可能需要了解记录器对错误的看法,以便进行故障排除,因此您可能需要以下内容:

except (Exception, psycopg2.DatabaseError) as error :
        logger.error("%s transction error Reverting all other operations of a transction ", error)
        if connection is not None:
            connection.rollback()


你的连接没有。您已经在
finally
语句中选中了None,那么为什么不选中
except
语句呢?这意味着没有执行查询?它将要离开街区吗?所以,如果连接不是None,我应该加上:除了block之外,还有什么?