0% found this document useful (0 votes)
18 views14 pages

PostgreSQL Player and Team Queries

The document contains Python scripts that connect to a PostgreSQL database using the psycopg2 library to perform various queries related to players, teams, students, referees, and books. It includes functions to check for prime jersey numbers, manipulate strings, and handle database connections with error management. The scripts read input from text files and execute SQL queries to retrieve and print specific information based on the data stored in the database.

Uploaded by

9878.aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

PostgreSQL Player and Team Queries

The document contains Python scripts that connect to a PostgreSQL database using the psycopg2 library to perform various queries related to players, teams, students, referees, and books. It includes functions to check for prime jersey numbers, manipulate strings, and handle database connections with error management. The scripts read input from text files and execute SQL queries to retrieve and print specific information based on the data stored in the database.

Uploaded by

9878.aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Week 7

import psycopg2
import sys

import os

database = [Link][1] #name of the database is obtained from the command line argument

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

def is_prime(i):

if i==2:

return True

if i<2:

return False
for j in range(2,i):

if i%j == 0:

return False

return True

conn = [Link](database = database, user = user, password = password, host = host, port = port)

cur = [Link]()

[Link](''' select [Link], p.jersey_no, [Link]

from players p, teams t

where t.team_id = p.team_id

order by [Link] desc, [Link] desc''')

result = [Link]()

for i in result:

if is_prime(i[1]):

print(i[0],i[2],sep = ", ")


import sys
import os

import psycopg2

database = [Link][1]

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')
alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

conn = [Link](database = database, user = user, password = password, host = host, port = port)

q = "select team_id from teams where jersey_home_color != jersey_away_color order by team_id" cur =

[Link]()

[Link](q)

res = [Link]()

for r in res:

#print(r)

s = ''

for i in r[0]:

if [Link]():

s += alph[(([Link](i)+7)%26)]

else:

s += str((int(i)+7)%10)

print(s)
f = open("[Link]", "r")
pl = [Link]()

[Link]()

import sys

import os

import psycopg2

database = [Link][1]
user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

conn = [Link](database = database, user = user, password = password, host = host, port = port)

c = [Link]()

[Link]("select jersey_no from players where name = %s",(pl,))

res = [Link]()

print(res[0][0])
import psycopg2
import os

import sys

database = [Link][1]

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

f = open("[Link]","r")

n = [Link]()

[Link]
conn = [Link](database=database,user=user,password=password,host=host,port=port)

c = [Link]()

[Link]('''select roll_no from students where students.student_fname = %s''',(n,))

res = [Link]()

print(res[0][0])

import psycopg2
import sys

import os

database = [Link][1]

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

f = open("[Link]","r")

t = [Link]()

[Link]()

conn = [Link](database= database,user=user, password= password,host=host,port=port)

c= [Link]()

[Link](''' select playground from teams where teams.team_id = %s''',(t,))

res = [Link]()

print(res[0][0])

import psycopg2

import sys

import os

f = open("[Link]","r")

n = [Link]()

[Link]()

database = [Link][1]

user = [Link]('PGUSER')
password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

def status(i):

if i%2 == 0:

print("Even")

else:

print('Odd')

con = [Link](database=database,user=user,password=password,host=host,port=port)

c= [Link]()

[Link]('''select d.department_name,[Link] from departments d, students s where d.department_code =


s.department_code and s.student_fname = %s''',(n,))

res = [Link]()

print(n, end=',')

print(res[0][0],end=',')

status(res[0][1].year)

import psycopg2,os,sys

f = open('[Link]','r')

name = [Link]()

database = [Link][1]

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

conn=None

try:

conn = [Link](database=database,user=user,password=password,host=host,port=port)

cur = [Link]()

[Link]("SELECT [Link] FROM referees r, match_referees mr, matches m WHERE r.referee_id = [Link] AND
mr.match_num = m.match_num AND m.match_date = %s",(name,))
result = [Link]()

for i in result:

str = i[0].split()

l = len(str)

print(str[-1],end=' ')

for x in str[:-1]:

print(x[0]+'. ',end='')

[Link]()

except(Exception,[Link]) as error:

print(error)

finally:

if conn is not None:

[Link]()

import psycopg2
import sys

import os

import math

conn=None

f=open('[Link]','r')

s=[Link]()

conn= [Link](database=[Link][1],user=[Link]('PGUSER'),
password=[Link]('PGPASSWORD'),host=[Link]('PGHOST'),port=[Link]('PGPORT'))

curr=[Link]()

[Link]("select sum(host_team_score) from matches where host_team_score > guest_team_score and host_team_id
in ( select team_id from teams where name like '{}%')".format(s))

row=[Link]()

y=int(row[0])

p=[Link]

deg=y*10*(p/180)

v=round([Link](deg),2)
print(v)

[Link]()

[Link]()

import psycopg2
import os

import sys

f = open('[Link]','r')

x = [Link]()

[Link]()

year = 2000+4*int(x)+4

database = [Link][1]

user = [Link]('PGUSER')

password = [Link]('PGPASSWORD')

host = [Link]('PGHOST')

port = [Link]('PGPORT')

conn = None

try:

conn = [Link](database = database,user=user, password = password, host = host, port = port)

cur = [Link]()

[Link]('''select ISBN_no from book_catalogue where year = %s''',(year,))

result = [Link]()

# print(result)

for i in result:

print(i[0])
[Link]()

except([Link]) as error:

print(error)

finally:

if conn is not None:

[Link]()
SELECT [Link], [Link], [Link], [Link]
FROM players p, teams t, managers m

WHERE p.team_id = t.team_id AND m.team_id = t.team_id AND p.jersey_no = ‘59’

You might also like