Last active
June 6, 2020 18:39
-
-
Save alexamy/b8b2165462e362606242ddbcf0165ebb to your computer and use it in GitHub Desktop.
Решение задания № 6 по теме "Знакомство с СУБД PostgreSQL" по предмету "Базы данных"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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