Skip to content

Instantly share code, notes, and snippets.

@alexamy
Last active June 6, 2020 18:39
Show Gist options
  • Save alexamy/b8b2165462e362606242ddbcf0165ebb to your computer and use it in GitHub Desktop.
Save alexamy/b8b2165462e362606242ddbcf0165ebb to your computer and use it in GitHub Desktop.
Решение задания № 6 по теме "Знакомство с СУБД PostgreSQL" по предмету "Базы данных"
import psycopg2
# 6. Сколько маршрутов обслуживают самолёты каждого типа?
query = """select ad.aircraft_code, count(distinct f.flight_no)
from aircrafts_data ad
left join flights f
using (aircraft_code)
group by ad.aircraft_code;"""
connection = psycopg2.connect(
host='scilink.ru',
port='5432',
database='demo',
user='demo',
password='<fpsLfyys[211'
)
cursor = connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
out = open('db_result.csv', 'w')
for row in rows:
print(f'{row[0]} | {row[1]}')
out.write(f'{row[0]}, {row[1]}\n')
out.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment