1.连接
def connpsycopg(): conn = psycopg2.connect(database="postgres", user="postgres", password="123", host="127.0.0.1", port="5433") return conn
2. 使用
def getYear(): conn = connpsycopg() cursor = conn.cursor() location_sql = "select distinct(year) from origin order by year desc" cursor.execute(location_sql) data = cursor.fetchall() bc = [x[0] for x in data] # bc.sort() print(bc) conn.close() return bc