diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f1c90 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +database.ini diff --git a/IT_company_explanation.txt b/IT_company_explanation.txt new file mode 100644 index 0000000..07e994d --- /dev/null +++ b/IT_company_explanation.txt @@ -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. diff --git a/It company ERD.JPG b/It company ERD.JPG new file mode 100644 index 0000000..523ea62 Binary files /dev/null and b/It company ERD.JPG differ diff --git a/__pycache__/config.cpython-310.pyc b/__pycache__/config.cpython-310.pyc new file mode 100644 index 0000000..5e18450 Binary files /dev/null and b/__pycache__/config.cpython-310.pyc differ diff --git a/config.py b/config.py new file mode 100644 index 0000000..26f55c9 --- /dev/null +++ b/config.py @@ -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()) \ No newline at end of file diff --git a/conncet.py b/conncet.py new file mode 100644 index 0000000..9125dca --- /dev/null +++ b/conncet.py @@ -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') + diff --git a/exercise5.py b/exercise5.py new file mode 100644 index 0000000..a98ef9f --- /dev/null +++ b/exercise5.py @@ -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") diff --git a/it_company.sql b/it_company.sql new file mode 100644 index 0000000..471e978 --- /dev/null +++ b/it_company.sql @@ -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; \ No newline at end of file diff --git a/pagila.png b/pagila.png new file mode 100644 index 0000000..c126648 Binary files /dev/null and b/pagila.png differ