Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database.ini
6 changes: 6 additions & 0 deletions IT_company_explanation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-In the table 'employees', each employee has an id, name and responsibility id,which is a foreign key
referring to the same column in table 'responsibility'.
Each employee has also a team id , which is also a foreign key references the team id in table teams

- In the table 'teams' , each team has an id , a product , a customer.
Last two reference the same columns in tables products and customers respectively.
Binary file added It company ERD.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __pycache__/config.cpython-310.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/python
from configparser import ConfigParser

def config(filename='database.ini', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)

# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))

return db

# print (config())
39 changes: 39 additions & 0 deletions conncet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/python
import psycopg2
from config import config

def connect(sql):
""" Connect to the PostgreSQL database server and run an sql command and print the results"""
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
# execute a statement

cur.execute(sql)

# return the command results :
result = cur.fetchall()
for r in result:
print (r)
# print(result)

# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')

if __name__ == '__main__':
connect('select * from actor')
connect('select * from category limit 1')
connect('select * from address limit 50')

51 changes: 51 additions & 0 deletions exercise5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python
import psycopg2
# from config import config

def connect(sql):
""" Connect to the PostgreSQL database server and run an sql command and print the results"""
conn = None
try:

print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(
host='localhost',
database='PyCoders',
user='postgres',
password='0000' )


conn.autocommit = True
cur = conn.cursor()


cur.execute(sql)


# return the command results :
result = cur.fetchall()
for r in result:
print (r)


# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')

if __name__ == '__main__':
connect("CREATE TABLE IF NOT EXISTS students( student_id int PRIMARY KEY , name varchar(50))")
connect("CREATE TABLE IF NOT EXISTS teachers( teacher_id int PRIMARY KEY , name varchar(50))")
connect("INSERT INTO students (student_id , name) VALUES (1, 'Henry')")
connect("INSERT INTO students (student_id , name) VALUES (2, 'Y')")
connect("INSERT INTO students (student_id , name) VALUES (3, 'X')")
connect("INSERT INTO teachers (teacher_id , name) VALUES (1, 'John')")
connect("INSERT INTO teachers (teacher_id, name) VALUES (2, 'X')")
connect("INSERT INTO teachers (teacher_id, name) VALUES (3, 'Y')")

connect("SELECT * FROM students")
connect("SELECT * FROM teachers")
90 changes: 90 additions & 0 deletions it_company.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- This script was generated by a beta version of the ERD tool in pgAdmin 4.
-- Please log an issue at https://redmine.postgresql.org/projects/pgadmin4/issues/new if you find any bugs, including reproduction steps.
BEGIN;


CREATE TABLE IF NOT EXISTS public.teams
(
team_id integer,
product_id integer,
customer_id integer
);

CREATE TABLE IF NOT EXISTS public.responsibilitiy
(
responsibilitiy_id integer NOT NULL,
title character varying(50),
PRIMARY KEY (responsibilitiy_id)
);

CREATE TABLE IF NOT EXISTS public.customers
(
customer_id integer,
name character varying(55),
domain_id integer,
PRIMARY KEY (customer_id)
);

CREATE TABLE IF NOT EXISTS public.products
(
product_id integer,
product_description text,
PRIMARY KEY (product_id)
);

CREATE TABLE IF NOT EXISTS public.employees
(
member_id integer,
name character varying,
team_id integer,
responsibilitiy_id integer,
PRIMARY KEY (member_id)
);

CREATE TABLE IF NOT EXISTS public.domains
(
domain_id integer,
domain_name character varying,
PRIMARY KEY (domain_id)
);

ALTER TABLE IF EXISTS public.teams
ADD FOREIGN KEY (product_id)
REFERENCES public.products (product_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public.teams
ADD FOREIGN KEY (customer_id)
REFERENCES public.customers (customer_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public.customers
ADD FOREIGN KEY (domain_id)
REFERENCES public.domains (domain_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public.employees
ADD CONSTRAINT responsibilitiy FOREIGN KEY (responsibilitiy_id)
REFERENCES public.responsibilitiy (responsibilitiy_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public.employees
ADD FOREIGN KEY (team_id)
REFERENCES public.teams (team_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;

END;
Binary file added pagila.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.