Python 3.x 如何关闭当前函数中另一个函数中定义的数据库连接
''' def数据库_连接(): 尝试: #PostgreSQL连接 Postgres_connection=psycopg2.connect(user=“Postgres”,password=“password”,host=“127.0.0.1”, port=“5432”,database=“Maps”) Postgres\u cursor=Postgres\u connection.cursor() 除了(异常,psycopg2.Error)作为错误1: 打印(“连接到PostgreSQL时出错”,错误1) 返回Postgres\u连接,Postgres\u光标Python 3.x 如何关闭当前函数中另一个函数中定义的数据库连接,python-3.x,postgresql-9.3,Python 3.x,Postgresql 9.3,''' def数据库_连接(): 尝试: #PostgreSQL连接 Postgres_connection=psycopg2.connect(user=“Postgres”,password=“password”,host=“127.0.0.1”, port=“5432”,database=“Maps”) Postgres\u cursor=Postgres\u connection.cursor() 除了(异常,psycopg2.Error)作为错误1: 打印(“连接到PostgreSQL时
def Database_connection_close():
Postgres_connection=Database_connection()
Postgres_connection.close()
“”“def Database_connection():try:#PostgreSQL connection Postgres_connection=psycopg2.connect(user=“Postgres”,password=“password”,host=“127.0.0.1”,port=“5432”,Database=“Maps”)Postgres_cursor=Postgres_connection.cursor(),除了(例外,psycopg2.Error)as error1:print(“连接到PostgreSQL时出错”,error1)返回Postgres_连接,Postgres_cursorabove是我在另一个函数中尝试关闭连接的连接函数,出现错误。请更具体一些我在数据库_连接()中定义了Postgres数据库连接函数我想在database_connection_close()函数中关闭该数据库连接,但当我尝试使用上面提到的代码时,出现了错误。