<gh_stars>0
'''
    <NAME>
    olmympics.py
    <NAME>
    Adapted by code from
'''

import psycopg2

from config import password
from config import database
from config import user

# Connect to the database
try:
    connection = psycopg2.connect(database=database, user=user, password=password)
except Exception as e:
    print(e)
    exit()

# cursor object to iterate over the rows generated by query

#Lists all the NOCs and their number of gold medals won,
#in decreasing order of the number of gold medals.
try:
    cursor = connection.cursor()
    query = '''SELECT medal.NOC, medal.gold 
               FROM medal 
               WHERE medal.gold IS NOT NULL 
               ORDER BY medal.gold DESC'''
    cursor.execute(query)
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== NOCS and its number of gold medals=====')
for row in cursor:
    print(row[0], row[1])
print()


#Lists all the athletes from a certain country,
# in case Kenya
search_string = 'Kenya'
query = '''SELECT athlete.full_name
           FROM athlete_data, athlete
           WHERE athlete.id = athlete_data.id 
           AND athlete_data.team = %s'''
try:
    cursor.execute(query, (search_string,))
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== Athletes from  ====='.format(search_string))
for row in cursor:
    print(row[0])
print()


#3rd query- Return all Female Athletes
try:
    cursor = connection.cursor()
    query = '''SELECT athlete.full_name
               FROM athlete, athlete_data
               WHERE athlete.id = athlete_data.id
               AND athlete_data.sex = 'F''''
    cursor.execute(query)
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== Names of athlete whose sex is Female =====')
for row in cursor:
    print(row[0])
print()

# Close connection to database
connection.close()

'''
    <NAME>
    olmympics.py
    <NAME>
    Adapted by code from
'''

import psycopg2

from config import password
from config import database
from config import user

# Connect to the database
try:
    connection = psycopg2.connect(database=database, user=user, password=password)
except Exception as e:
    print(e)
    exit()

# cursor object to iterate over the rows generated by query

#Lists all the NOCs and their number of gold medals won,
#in decreasing order of the number of gold medals.
try:
    cursor = connection.cursor()
    query = '''SELECT medal.NOC, medal.gold 
               FROM medal 
               WHERE medal.gold IS NOT NULL 
               ORDER BY medal.gold DESC'''
    cursor.execute(query)
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== NOCS and its number of gold medals=====')
for row in cursor:
    print(row[0], row[1])
print()


#Lists all the athletes from a certain country,
# in case Kenya
search_string = 'Kenya'
query = '''SELECT athlete.full_name
           FROM athlete_data, athlete
           WHERE athlete.id = athlete_data.id 
           AND athlete_data.team = %s'''
try:
    cursor.execute(query, (search_string,))
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== Athletes from  ====='.format(search_string))
for row in cursor:
    print(row[0])
print()


#3rd query- Return all Female Athletes
try:
    cursor = connection.cursor()
    query = '''SELECT athlete.full_name
               FROM athlete, athlete_data
               WHERE athlete.id = athlete_data.id
               AND athlete_data.sex = 'F''''
    cursor.execute(query)
except Exception as e:
    print(e)
    exit()
# Iterates over cursor rows to print results
print('===== Names of athlete whose sex is Female =====')
for row in cursor:
    print(row[0])
print()

# Close connection to database
connection.close()
<NAME>     olmympics.py     <NAME>     Adapted by code from # Connect to the database # cursor object to iterate over the rows generated by query #Lists all the NOCs and their number of gold medals won, #in decreasing order of the number of gold medals. SELECT medal.NOC, medal.gold                 FROM medal                 WHERE medal.gold IS NOT NULL                 ORDER BY medal.gold DESC # Iterates over cursor rows to print results #Lists all the athletes from a certain country, # in case Kenya SELECT athlete.full_name            FROM athlete_data, athlete            WHERE athlete.id = athlete_data.id             AND athlete_data.team = %s # Iterates over cursor rows to print results #3rd query- Return all Female Athletes SELECT athlete.full_name                FROM athlete, athlete_data                WHERE athlete.id = athlete_data.id                AND athlete_data.sex = 'F # Iterates over cursor rows to print results # Close connection to database